为什么调用requestWhenInUseAuthorization不会提示用户允许访问位置?

77

在我的viewDidLoad方法中,我有以下代码

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];
}

请求已经发出,但用户没有收到提示?为什么?


此外,你现在所使用的检查操作系统版本的方式并不被推荐。更好的方法是使用 if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization:)]) - Jai Govindani
1个回答

295
你可能需要更新你的plist文件。这里有一个教程如何操作,简单易懂:
你需要在你的Info.plist文件中添加以下键之一:
NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

您还需要请求相应位置方法的授权,WhenInUseBackground。使用以下其中一个调用:

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

我还发现了一篇有用的文章:

iOS 8中位置服务无法工作

这篇答案详细介绍了如何更新你的plist文件

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>

发布代码之前,您可能需要自定义和拼写检查info.plist文件中的字典条目字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接