RequestWhenInUseAuthorization () does not work in iOS 8 with NSLocationWhenInUseUsageDescription Key in Info.plist

I am using the iOS SDK 8.1, trying to call the requestWhenInUseAuthorization () method to invite the user to grant access to my application. I imported CoreLocation.framework and added the NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription keys to info.plist. When I launched the application, it never asked me for access to the location. Below is my code, what did I miss?

import UIKit import CoreLocation import MapKit class ViewController: UIViewController, CLLocationManagerDelegate { override func viewDidLoad() { super.viewDidLoad() var manager = CLLocationManager() manager.delegate = self manager.desiredAccuracy = kCLLocationAccuracyBest let authorizationStatus = CLLocationManager.authorizationStatus() switch authorizationStatus { case .Authorized: println("authorized") case .AuthorizedWhenInUse: println("authorized when in use") case .Denied: println("denied") case .NotDetermined: println("not determined") case .Restricted: println("restricted") } manager.requestWhenInUseAuthorization() manager.startUpdatingLocation() } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { let location = locations[0] as CLLocation println("Latitude: \(location.coordinate.latitude). Longitude: \(location.coordinate.longitude).") } func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { switch status { case .Authorized: println("authorized") case .AuthorizedWhenInUse: println("authorized when in use") case .Denied: println("denied") case .NotDetermined: println("not determined") case .Restricted: println("restricted") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 

The console only showed "undefined" once and nothing more. So I went to iPhone Simulator => Settings => Privacy => Location => My application. He showed me 3 options: “Never”, “When using the application”, “Always”. But nothing was chosen.

+4
ios8 swift core-location
Dec 29 '15 at 0:12
source share
3 answers

The problem is resolved. My manager was declared as a local var inside the viewDidLoad() method, but it had to be a class.

After I moved the manager declaration from viewDidLoad() , my application worked.

You do not know exactly how manager.requestWhenInUseAuthorization() works behind the scenes and why exactly the manager defined in viewDidLoad() does not work. I hope someone who knows this detail enlightens me.

+18
Dec 30 '15 at 2:00
source share

Setting these properties in viewWillAppear instead of viewDidLoad fixed it for me, thanks!

 override func viewWillAppear(animated: Bool) { locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } 
+1
Nov 23 '17 at 19:13
source share

I also ran into the same problem. I added two keys to info.plist.

enter image description here

NSLocationWhenInUseUsageDescription key is supported in iOS 8.0 and later. If your Info.plist file contains both this key and the NSLocationUsageDescription key, the system uses this key and ignores the NSLocationUsageDescription key.

Rebuild code:

 let locationManager: CLLocationManager = CLLocationManager() let authorizationStatus = CLLocationManager.authorizationStatus() override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self if(authorizationStatus == .Denied) { print("DENIED") locationManager.requestWhenInUseAuthorization() } } override func viewWillAppear(animated: Bool) { locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } 

Clean the project and run it again.

0
Mar 31 '16 at 12:34
source share



All Articles