I am using iOS SDK 8.1 trying to call requestWhenInUseAuthorization() method to prompt user to grant access to my app. I imported CoreLocation.framework, and added NSLocatio
Problem solved. My manager
was declared as local var inside viewDidLoad()
method, but it should've been a class level property.
After I moved manager
declaration out of viewDidLoad()
, my app worked.
Not sure how exactly manager.requestWhenInUseAuthorization()
work behind the scene and why exactly manager
defined within viewDidLoad()
not work. Hope someone who knows this detail enlighten me.
I also faced the same problem. I have added two keys together in info.plist.
NSLocationWhenInUseUsageDescription key is supported in iOS 8.0 and later. If your Info.plist file includes both this key and the NSLocationUsageDescription key, the system uses this key and ignores the NSLocationUsageDescription key.
Rearranged the 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 again.
Setting these properties in the viewWillAppear instead of the viewDidLoad fixed it for me, thanks!
override func viewWillAppear(animated: Bool) {
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}