为什么requestWhenInUseAuthorization不会提示用户访问该位置?


74

用我的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

Answers:


281

您可能需要更新plist文件。这是一个如何做到教程,快速又肮脏:

您需要做的是将以下键之一添加到Info.plist文件中:

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

您还需要请求相应位置方法的授权,WhenInUse或者Background。使用以下调用之一:

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

还有一篇我发现有帮助的帖子:

定位服务在iOS 8中不起作用

该答案详细说明了如何更新plist文件

将以下行之一添加到您的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文件中包含的词典条目的字符串,然后再发送代码

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.