位置服务在iOS 8中无法工作。
位置服务在iOS 8中无法工作。
我的应用在iOS 7上工作得很好,但在iOS 8 SDK上工作不正常。
CLLocationManager
没有返回位置,我也没在设置->位置服务下找到我的应用。我在Google上搜索了这个问题,但是没有任何结果。可能出了什么问题?
我曾经也因同样的问题困扰了很久。Xcode给出的错误提示:
尝试在未请求位置授权的情况下启动
MapKit
位置更新。必须首先调用-[CLLocationManager requestWhenInUseAuthorization]
或-[CLLocationManager requestAlwaysAuthorization]
。
但是即使你实现了上述方法之一,如果info.plist
没有NSLocationAlwaysUsageDescription
或NSLocationWhenInUseUsageDescription
的条目,它也不会提示用户
请将下面的行添加到info.plist
中,其中字符串值表示需要访问用户位置的原因
NSLocationWhenInUseUsageDescription This application requires location services to work NSLocationAlwaysUsageDescription This application requires location services to work
我认为这些条目可能从我开始使用Xcode 5时就缺失了。我猜测Xcode 6可能会为这些键添加默认条目,但还没有确认。
您可以在这里找到有关这两个设置的更多信息
最终我解决了自己的问题。
显然在iOS 8 SDK中,需要在开始位置更新之前调用CLLocationManager
上的requestAlwaysAuthorization
(用于后台位置)或requestWhenInUseAuthorization
(仅在前台使用位置)。
还需要在Info.plist
中添加NSLocationAlwaysUsageDescription
或NSLocationWhenInUseUsageDescription
键,并提供要在提示中显示的消息。添加这些解决了我的问题。
有关更详细的信息,请查看:Core-Location-Manager-Changes-in-ios-8