iOS 8 requestWhenInUseAuthorization没有popup

我试图让我的AppProject iOS 8准备就绪。 我读了很多

[_locationManager requestWhenInUseAuthorization]; 

和plist中的入口

 NSLocationWhenInUseUsageDescription 

所以我改变了所有必要的代码行。

它工作正常,但现在我已经从我的iOS 7基地再次复制我的项目,包括新function。 但是,当我对iOS8位置隐私进行更改时,Popup不再显示。

我的代码工作,直到我复制。

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

这是我的电话

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

我怎样才能解决这个问题?

从文档

NSLocationWhenInUseUsageDescription(String – iOS)描述了应用程序在前台运行时正常访问用户位置的原因。 当您的应用使用位置服务直接跟踪用户的当前位置时,请添加此密钥。 此密钥不支持使用位置服务来监视区域或使用重要的位置更改服务来监视用户的位置。 当请求使用位置服务的许可时,系统在显示给用户的警报面板中包括该键的值。

当您使用CLLocationManager类的requestWhenInUseAuthorization方法请求位置服务的授权时,此项是必需的。 如果在不包含此密钥的情况下调用requestWhenInUseAuthorization方法时密钥不存在,系统将忽略您的请求。

此密钥在iOS 8.0及更高版本中受支持。 如果Info.plist文件包含此密钥和NSLocationUsageDescription密钥,则系统使用此密钥并忽略NSLocationUsageDescription密钥。

在这里阅读。

我发现将这个键添加到你的info.plist最简单的方法是右键单击你的info.plist并select

打开As->源代码

然后 </dict></plist> 之前的末尾添加以下内容

 <key>NSLocationWhenInUseUsageDescription</key> <string></string> 

如果你想要的话,你可以在<string></string>之间添加一个文本,向用户描述为什么你要使用他/她的位置。 该文本将显示在警报的默认文本下。

尝试在Info.plist中编写NSLocationWhenInUseUsageDescription

iOS 8.3,Xcode 6.3.1,ARC启用

这个问题已经解决了,但是我有(2)笔记来添加我最近参与CLLocationManager。

1)您必须在您的* .plist文件中input以下密钥:

在这里输入图像说明

最常用的键具有通用的更多描述性名称,例如“隐私 – 位置使用说明”,这实际上是“NSLocationUsageDescription”键。

要查看“原始”键名称,请转到“* -Info.plist”,然后右键单击键列出的导航区域,如下所示:

在这里输入图像说明

你会得到以下结果:

在这里输入图像说明

与本文相关的三个键是:

在这里输入图像说明

2)确保在尝试请求授权或更新位置之前分配并初始化CLLocationManager的实现。

* .h文件:

 @interface SomeController : UIViewController <CLLocationManagerDelegate> @property (strong, nonatomic) CLLocationManager *locationManager; 

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

希望这可以节省一些时间! 谢谢。

这是一个小问题。 确保你将NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription键添加到主包plist,而不是你的testing目标之一!

有相同的问题,因为我实例化CLLocationManager在一个方法内的本地变种,解决了它使CLLocationManager类属性。
过了一会儿,我在这里find了解决办法,但是我把它留在这里,因为这是谷歌的第一个结果,希望我能为你节省一些时间:

requestWhenInUseAuthorization()不适用于iOS 8使用NSLocationWhenInUseUsageDescription键入Info.plist

关于苹果手表:

您需要将requestWhenInUseAuthorization键放在iPhone的Info.plist ,而不是WatchKit应用程序的。

这已经咬了我两次了。