Why requestWhenInUseAuthorization doesn't prompt the user for access to the location?

别等时光非礼了梦想. 提交于 2019-12-20 07:59:11

问题


In my viewDidLoad method I have

locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
locationManager.delegate = self; // we set the delegate of locationManager to self.
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];


if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
    [locationManager requestWhenInUseAuthorization];
}

And the request is called, but the user is not prompted? Why?


回答1:


You probably need to update your plist file. Here's a tutorial how to do it, quick & dirty:

You need to do is to add one of the following keys to your Info.plist file:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

You also need to request authorization for the corresponding location method, WhenInUse or Background. Use one of these calls:

[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]

There's also a post that I found helpful:

Location Services not working in iOS 8

This answer details how to update your plist file:

Add one of the following lines to your info.plist

<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>

You'll probably want to customize and spell-check the strings of the dictionary entry that goes in the info.plist file before shipping the code.



来源:https://stackoverflow.com/questions/31845450/why-requestwheninuseauthorization-doesnt-prompt-the-user-for-access-to-the-loca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!