首页 > 代码库 > iOS8中的定位服务

iOS8中的定位服务

iOS8中的定位服务

My app that worked fine in iOS 7 doesn‘t work with the iOS 8 SDK.

CLLocationManager doesn‘t return a location, and I don‘t see my app under Settings > Location Services either. I did a Google search on the issue but nothing came up, what could be wrong?

我的应用在iOS7上运行得好好的,但是到了iOS8上就出问题了.

在iOS8中,CLLocationManager并没有返回一个地址,而且,我也没有在Settings>Location Services中找到该服务,在Google上也没查到类似的问题,请问到底哪里出问题了?

 

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.

There also needs to be NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist with a message to be displayed in the prompt. Adding these solved my problem.

Hope it helps someone else.

最后我解决了这个问题.

在iOS8的sdk中,你需要额外的requestAlwaysAuthorization(用作后台定位)以及requestWhenInUseAuthorization(用作前台定位),在调用CLLocationManager更新地址之前你都需要调用上述两个方法.你还需要在Info.plist文件中额外的添加NSLocationAlwaysUsageDescription以及NSLocationWhenInUseUsageDescription,这么做了之后才解决了我的问题.

 

According to the Apple docs:

https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestWhenInUseAuthorization

As of iOS 8, the presence of a NSLocationWhenInUseUsageDescription or a NSLocationAlwaysUsageDescription key value in your app‘s Info.plist file is required. 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. The string you entered into the Info.plist will then be displayed in the ensuing dialog.

If the user grants permission, it‘s business as usual. If they deny permission, then the delegate is not informed of location updates.

根据苹果的官方文档:

https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestWhenInUseAuthorization

在iOS8上,你需要在你的plist文件中添加新的键值NSLocationWhenInUseUsageDescription或者NSLocationAlwaysUsageDescription.然后,每次定位的时候你都需要获取用户的授权信息,通过调用[self.myLocationManager requestWhenInUseAuthorization] 或者[self.myLocationManager requestAlwaysAuthorization],至于调用哪一个,这就根据你自己的需求了.那个你添加进plist文件中的键值信息会在确认的信息中有提示.

如果用户允许你定位,你可以像往常那样子使用定位功能.如果他们拒绝了,那么,这个定位的更新更能永远也不会被执行.

 

iOS8中的定位服务