CLLocationManagerWhenInUseAuthorization () request not working

I am trying to use location services in my iOS application, but for some reason requestWhenInUseAuthorization not working. When the user first uses the application, the request appears as usual, asking for permission, but then when you open the application for the second time, for some reason the didChangeAuthorizationStatus method didChangeAuthorizationStatus not called, so I can not display the user's current location on the map.

My code is below:

  override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib locationManager.delegate = self locationManager.requestWhenInUseAuthorization() var config:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() config.URLCache = NSURLCache(memoryCapacity: 2 * 1024 * 1024, diskCapacity: 10 * 1024 * 1024, diskPath: "MarkerData") markerSession = NSURLSession(configuration: config) } func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { if status == .AuthorizedWhenInUse { locationManager.startUpdatingLocation() mapView.delegate = self mapView.myLocationEnabled = true mapView.settings.myLocationButton = true } } 
+3
source share
1 answer

First you need to add NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription (if you want to use in the background) in the info.plist file. See the following image:

enter image description here

Next, in your fast file, you need to call either locationManager.requestWhenInUseAuthorization() or locationManager.requestAlwaysAuthorization() in your viewDidLoad() method.

Finally, you can do mapView.camera = GMSCameraPosition(target: locations.last!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) in your delegation method locationManager.

Code example:

 class ViewController: UIViewController, CLLocationManagerDelegate { var locationManager = CLLocationManager(); override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6) var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera) mapView.myLocationEnabled = true self.view = mapView locationManager.delegate = self locationManager.distanceFilter = kCLDistanceFilterNone locationManager.desiredAccuracy = kCLLocationAccuracyBest if #available(iOS 8.0, *) { print("iOS >= 8.0.0") locationManager.requestAlwaysAuthorization() } locationManager.startUpdatingLocation() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { println(locations.last) var mapView = self.view as! GMSMapView mapView.camera = GMSCameraPosition(target: locations.last!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) } } 

You can post this for more details on changing the LocationManager in iOS 8.

+10
source

All Articles