objective-c 为什么 requestWhenInUseAuthorization 不提示用户访问该位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31845450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:35:06  来源:igfitidea点击:

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

objective-cxcodeios8cllocationmanager

提问by spenf10

In my viewDidLoadmethod I have

在我的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];
}

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

并且调用了请求,但是没有提示用户?为什么?

回答by Adrian

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

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

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

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

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

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

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

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

There's also a post that I found helpful:

还有一篇我觉得很有帮助的帖子:

Location Services not working in iOS 8

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

This answer details how to update your plistfile:

此答案详细说明了如何更新您的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

<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.

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