本文翻译自:Location Services not working in iOS 8
My app that worked fine on iOS 7 doesn't work with the iOS 8 SDK. 我在iOS 7上运行良好的应用无法与iOS 8 SDK一起使用。
CLLocationManager
doesn't return a location, and I don't see my app under Settings -> Location Services either. CLLocationManager
不返回位置,在“设置” ->“ 位置服务”下也看不到我的应用程序。 I did a Google search on the issue, but nothing came up. 我对此问题进行了Google搜索,但没有发现任何问题。 What could be wrong? 有什么事吗
#1楼
参考:https://stackoom.com/question/1cxkz/定位服务在iOS-中不起作用
#2楼
I ended up solving my own problem. 我最终解决了自己的问题。
Apparently in iOS 8 SDK, requestAlwaysAuthorization
(for background location) or requestWhenInUseAuthorization
(location only when foreground) call on CLLocationManager
is needed before starting location updates. 显然,在iOS 8 SDK中,在开始位置更新之前,需要在CLLocationManager
上调用requestAlwaysAuthorization
(用于背景位置)或requestWhenInUseAuthorization
(仅用于前景位置)。
There also needs to be NSLocationAlwaysUsageDescription
or NSLocationWhenInUseUsageDescription
key in Info.plist
with a message to be displayed in the prompt. 在Info.plist
还需要NSLocationAlwaysUsageDescription
或NSLocationWhenInUseUsageDescription
键,并在提示中显示一条消息。 Adding these solved my problem. 添加这些解决了我的问题。
Hope it helps someone else. 希望它可以帮助别人。
EDIT: For more extensive information, have a look at: Core-Location-Manager-Changes-in-ios-8 编辑:有关更多信息,请参阅: Core-Location-Manager-Changes-in-ios-8
#3楼
I was pulling my hair out with the same problem. 我也遇到同样的问题。 Xcode gives you the error: Xcode给您错误:
Trying to start
MapKit
location updates without prompting for location authorization. 尝试启动MapKit
位置更新而没有提示位置授权。 Must call-[CLLocationManager requestWhenInUseAuthorization]
or-[CLLocationManager requestAlwaysAuthorization]
first. 必须首先调用-[CLLocationManager requestWhenInUseAuthorization]
或-[CLLocationManager requestAlwaysAuthorization]
。
But even if you implement one of the above methods, it won't prompt the user unless there is an entry in the info.plist for NSLocationAlwaysUsageDescription
or NSLocationWhenInUseUsageDescription
. 但是,即使您实现上述方法之一,也不会提示用户,除非在info.plist中有NSLocationAlwaysUsageDescription
或NSLocationWhenInUseUsageDescription
。
Add the following lines to your info.plist where the string values represent the reason you you need to access the users location 将以下行添加到info.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>
I think these entries may have been missing since I started this project in Xcode 5. I'm guessing Xcode 6 might add default entries for these keys but have not confirmed. 我认为自从我在Xcode 5中启动该项目以来,这些条目可能已经丢失了。我猜Xcode 6可能会为这些键添加默认条目,但尚未确认。
You can find more information on these two Settings here 您可以在此处找到有关这两个设置的更多信息
#4楼
According to the Apple docs: 根据Apple文档:
- https://developer.apple.com/documentation/corelocation/requesting_permission_to_use_location_services https://developer.apple.com/documentation/corelocation/requesting_permission_to_use_location_services
- https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuse授权
As of iOS 8, the presence of a NSLocationWhenInUseUsageDescription
or a NSLocationAlwaysUsageDescription
key value in your app's Info.plist file is required. 从iOS 8开始,需要在应用程序的Info.plist文件中存在NSLocationWhenInUseUsageDescription
或NSLocationAlwaysUsageDescription
键值。 It's then also necessary to request permission from the user prior to registering for location updates, either by calling [self.myLocationManager requestWhenInUseAuthorization]
or [self.myLocationManager requestAlwaysAuthorization]
depending on your need. 然后,还需要根据注册的需要,通过调用[self.myLocationManager requestWhenInUseAuthorization]
或[self.myLocationManager requestAlwaysAuthorization]
,在注册位置更新之前向用户请求权限。 The string you entered into the Info.plist will then be displayed in the ensuing dialog. 您在Info.plist中输入的字符串将显示在随后的对话框中。
If the user grants permission, it's business as usual. 如果用户授予许可,则照常营业。 If they deny permission, then the delegate is not informed of location updates. 如果他们拒绝许可,则不会将位置更新通知给代理。
#5楼
To ensure that this is backwards compatible with iOS 7, you should check whether the user is running iOS 8 or iOS 7. For example: 为了确保它与iOS 7向后兼容,您应该检查用户运行的是iOS 8还是iOS7。例如:
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
//In ViewDidLoad
if(IS_OS_8_OR_LATER) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
#6楼
Solution with backward compatibility: 向后兼容的解决方案:
SEL requestSelector = NSSelectorFromString(@"requestWhenInUseAuthorization");
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
[self.locationManager respondsToSelector:requestSelector]) {
[self.locationManager performSelector:requestSelector withObject:NULL];
} else {
[self.locationManager startUpdatingLocation];
}
Setup NSLocationWhenInUseUsageDescription key in your Info.plist 在Info.plist中设置NSLocationWhenInUseUsageDescription键