我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
当前回答
向后兼容解决方案:
SEL requestSelector = NSSelectorFromString(@"requestWhenInUseAuthorization");
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
[self.locationManager respondsToSelector:requestSelector]) {
[self.locationManager performSelector:requestSelector withObject:NULL];
} else {
[self.locationManager startUpdatingLocation];
}
在Info.plist中设置NSLocationWhenInUseUsageDescription键
其他回答
我也因为同样的问题而抓狂。Xcode给出错误:
试图启动MapKit位置更新而没有提示 位置授权。必须调用-[CLLocationManager requestWhenInUseAuthorization]或-[CLLocationManager . request requestAlwaysAuthorization]。
但即使实现了上述方法之一,它也不会提示用户,除非信息中有条目。NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription。
添加以下行到您的信息。Plist,其中字符串值代表您需要访问用户位置的原因
<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>
我认为自从我在Xcode 5中开始这个项目以来,这些条目可能已经丢失了。我猜Xcode 6可能会为这些键添加默认条目,但还没有确认。
你可以在这里找到更多关于这两个设置的信息
为了访问用户在iOS中的位置。您需要添加两个键
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
进入信息。plist文件。
<key>NSLocationWhenInUseUsageDescription</key>
<string>Because I want to know where you are!</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Want to know where you are!</string>
请看下图。
保持可可钥匙信息总是在你的指尖为这些更新,这里是链接:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26
享受。
Swift开发者常犯的一个错误:
首先确保你添加了一个值到plist为NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription。
如果你仍然没有看到一个窗口弹出要求授权,看看你是否把行var locationManager = CLLocationManager()在你的视图控制器的viewDidLoad方法。如果这样做,那么即使调用locationManager.requestWhenInUseAuthorization(),也不会显示任何内容。这是因为在viewDidLoad执行后,locationManager变量被释放(清除)。
解决方案是在类方法的顶部找到var locationManager = CLLocationManager()行。
我最终解决了自己的问题。
显然,在iOS 8 SDK中,在开始位置更新之前需要调用CLLocationManager上的requestAlwaysAuthorization(用于后台位置)或requestWhenInUseAuthorization(仅用于前台位置)。
在Info中还需要NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription键。Plist并在提示符中显示一条消息。加上这些就解决了我的问题。
更多详细信息,请查看:Core-Location-Manager-Changes-in-ios-8