位置服务:LocationAlways 与 LocationWhenInUse

Location service: LocationAlways vs LocationWhenInUse

这是 iOS 应用程序中的一个奇怪行为。 这是与我的问题相关的代码:

正在初始化位置信息:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestWhenInUseAuthorization];

请求位置功能时:

if ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorized) {
    // Give a message to the user: NO GPS HERE!
}

在 app.plist 里面,我放了一个键:NSLocationWhenInUseUsageDescription,带有一个字符串。

由于某种我不明白的原因,它不起作用。我被告知这个应用程序不允许使用 GPS,我必须更改隐私设置,我去按照要求更改隐私设置,我回来再试一次,并收到相同的消息……。并陷入无限循环。

另一方面,如果我放一个键:NSLocationAlwaysUsageDescription,在 app.plist 里面有一个字符串;不更改代码中的任何内容。 然后允许我的应用程序使用位置功能 (LocationAlways)。 虽然这比完全没有 GPS 好,但我的应用程序最终使用了 -LocationAlways- 模式,而它很乐意使用 —LocationWhenInUse- 模式。

有人遇到同样的问题并找到了解决方案吗?

因为 kCLAuthorizationStatusAuthorized 在 iOS 8

中已弃用

文档

kCLAuthorizationStatusAuthorized This app is authorized to use location services.

Available in iOS 2.0 and later. Deprecated in iOS 8.0

看看enum的文档,就知道为什么kCLAuthorizationStatusAuthorized有效

typedef enum {
   kCLAuthorizationStatusNotDetermined = 0,
   kCLAuthorizationStatusRestricted,
   kCLAuthorizationStatusDenied,
   kCLAuthorizationStatusAuthorized,
   kCLAuthorizationStatusAuthorizedAlways = kCLAuthorizationStatusAuthorized,
   kCLAuthorizationStatusAuthorizedWhenInUse 
} CLAuthorizationStatus;

因此,如果您请求 whenInUse,请使用此密钥 kCLAuthorizationStatusAuthorizedWhenInUse