objective-c iOS 8 requestWhenInUseAuthorization 没有弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24850128/
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
iOS 8 requestWhenInUseAuthorization no Popup
提问by Marcus
I tried to make my AppProject iOS 8 ready. I had read a lot about
我试图让我的 AppProject iOS 8 准备就绪。我已经阅读了很多关于
[_locationManager requestWhenInUseAuthorization];
and the entry in plist
和 plist 中的条目
NSLocationWhenInUseUsageDescription
So I changed all the necessary code lines.
所以我更改了所有必要的代码行。
It works fine, but now I have copied my project again from my iOS 7 base to include new features. But when I make the changes for the iOS8 Location Privacy the Popup doesn't appear anymore.
它工作正常,但现在我再次从我的 iOS 7 基础复制了我的项目以包含新功能。但是当我对 iOS8 位置隐私进行更改时,弹出窗口不再出现。
My code worked until I copied.
我的代码在我复制之前一直有效。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string>tolle sache </string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>fapporite.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
and here is my call
这是我的电话
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
_UserLocation = [[CLLocation alloc]init];
_locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest; // setting the accuracy
[_locationManager requestWhenInUseAuthorization]; // iOS 8 MUST
[_locationManager startUpdatingLocation]; //requesting location updates
NSLog(@"passed initwithcode");
}
return self;
}
How can I fix this?
我怎样才能解决这个问题?
回答by Groot
From the documentation
从文档
NSLocationWhenInUseUsageDescription (String - iOS) describes the reason why the app accesses the user's location normally while running in the foreground. Include this key when your app uses location services to track the user's current location directly. This key does not support using location services to monitor regions or monitor the user's location using the significant location change service. The system includes the value of this key in the alert panel displayed to the user when requesting permission to use location services.
This key is required when you use the requestWhenInUseAuthorization method of the CLLocationManager class to request authorization for location services. If the key is not present when you call the requestWhenInUseAuthorization method without including this key, the system ignores your request.
This 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.
NSLocationWhenInUseUsageDescription (String - iOS) 描述了应用在前台运行时正常访问用户位置的原因。当您的应用使用位置服务直接跟踪用户的当前位置时,请包含此键。该密钥不支持使用位置服务来监控区域或使用显着位置更改服务来监控用户的位置。当请求使用位置服务的许可时,系统会在向用户显示的警报面板中包含此键的值。
当您使用CLLocationManager 类的requestWhenInUseAuthorization 方法请求位置服务授权时,需要此密钥。如果在您调用 requestWhenInUseAuthorization 方法而不包含此键时该键不存在,则系统将忽略您的请求。
iOS 8.0 及更高版本支持此密钥。如果您的 Info.plist 文件同时包含此键和 NSLocationUsageDescription 键,系统将使用此键并忽略 NSLocationUsageDescription 键。
Read about it here.
在这里阅读。
I find that the easiest way to add this key to your info.plist is to right click you info.plist and choose
我发现将此密钥添加到您的 info.plist 的最简单方法是右键单击您的 info.plist 并选择
Open As->Source Code
打开为->源代码
and then add the following in the end before</dict></plist>
然后添加下面到底前</dict></plist>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
If you want you can add a text in between <string></string>that describes to the user why you want to use his/hers location. This text will show up under the default text in the alert.
如果您愿意,您可以在中间添加一个文本,<string></string>向用户描述您为什么要使用他/她的位置。此文本将显示在警报中的默认文本下。
回答by Masatsugu Hosoi
Try writing a NSLocationWhenInUseUsageDescription in Info.plist
尝试在 Info.plist 中编写 NSLocationWhenInUseUsageDescription
回答by serge-k
iOS 8.3, Xcode 6.3.1, ARC enabled
iOS 8.3、Xcode 6.3.1、ARC 已启用
The question has been resolved, but I have (2) notes to add from my recent involvement with CLLocationManager.
问题已解决,但我最近参与 CLLocationManager 时有 (2) 个注释要添加。
1) You must have the following keys entered into your *.plist file:
1) 您必须在 *.plist 文件中输入以下密钥:


Most commonly used keys have generic more descriptive names, such as "Privacy - Location Usage Description", which really is the "NSLocationUsageDescription" key.
最常用的键具有通用的更具描述性的名称,例如“隐私 - 位置使用说明”,这实际上是“NSLocationUsageDescription”键。
To see the "raw" key names go to "*-Info.plist" and right click in the Navigator area where the keys are listed, see below:
要查看“原始”键名称,请转到“*-Info.plist”并右键单击列出键的导航器区域,如下所示:


And you get the following results:
你会得到以下结果:


The three keys that are related to this article are:
与本文相关的三个键是:


2) Make certain that you allocate and initialize your implementation of CLLocationManager before you try to request authorization or update location.
2) 在您尝试请求授权或更新位置之前,确保您分配并初始化了 CLLocationManager 的实现。
*.h file:
*.h 文件:
@interface SomeController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
*.m file:
*.m 文件:
- (IBAction)locationButton:(UIButton *)sender
{
if (self.locationManager == nil)
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
else
{
nil;
}
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
else
{
nil;
}
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
Hope this saves someone time! Thanks.
希望这可以节省某人的时间!谢谢。
回答by smileBot
Here's a little gotcha. Make sure you're adding the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription keys to the main bundle plist and not one of your test target plists!
这是一个小问题。确保您将 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 键添加到主包 plist 而不是您的测试目标 plist 之一!
回答by Mark E
Had the same problem caused because I was instantiating CLLocationManagerin a local var inside a method, solved it making the CLLocationManagera class property.
After a while I found the solution here, but I'm leaving it here since this is the first result in google, hope I save you some time:
有同样的问题,因为我CLLocationManager在方法内的本地 var 中实例化,解决了它使CLLocationManager类属性。
过了一会儿我在这里找到了解决方案,但我把它留在这里,因为这是谷歌的第一个结果,希望我能节省你一些时间:
requestWhenInUseAuthorization() 在 Info.plist 中使用 NSLocationWhenInUseUsageDescription 键在 iOS 8 中不起作用
回答by HenryRootTwo
Pertaining to the Apple Watch:
Apple Watch 的相关资料:
You need to put the requestWhenInUseAuthorizationkey in the iPhone's Info.plist, not the WatchKit App's.
您需要将requestWhenInUseAuthorization密钥放在 iPhone 的 中Info.plist,而不是 WatchKit 应用程序中。
This has bitten me twice now.
这已经咬了我两次了。

