位置服务使用iOS10重新安装后停止工作

时间:2016-11-28 18:28:41

标签: ios objective-c core-location ios10

卸载iOS10应用后,位置服务停止工作。我在Info.plist中设置了描述,因此让我感到困惑的是为什么位置服务会停止工作。该应用未显示在“设置” - >“隐私” - >“位置服务”

<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs to use your location so that it may send it to your selected contacts.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs to use your location so that it may send it to your selected contacts.</string>

使用以下代码检查状态显示状态为未确定。

if ([CLLocationManager locationServicesEnabled]) {
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) {
        NSLog(@"RESTRICTED");
    } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        NSLog(@"DENIED");
    } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
        NSLog(@"NOT DETERMINED");
    } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) {
        NSLog(@"ALWAYS");
    } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
        NSLog(@"WHEN IN USE");
    }
}

修改 viewController viewDidLoad方法包含以下代码:

- (void)viewDidLoad {
    [self.locationManager requestAlwaysAuthorization];
    [self.locationManager requestWhenInUseAuthorization];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.delegate = self;

    if ([CLLocationManager locationServicesEnabled]) {
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) {
            NSLog(@"RESTRICTED");
        } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
            NSLog(@"DENIED");
        } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
            NSLog(@"NOT DETERMINED");
        } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) {
            NSLog(@"ALWAYS");
        } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) {
            NSLog(@"WHEN IN USE");
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您需要使用[yourLocationManager startUpdatingLocation],这会提示使用位置的提醒。

在此之前authorizationStatus将为kCLAuthorizationStatusNotDetermined

修改

self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[self.locationManager requestAlwaysAuthorization]; 
self.locationManager.delegate = self;

添加此

答案 1 :(得分:0)

您需要检查授权状态,如果是xxx,则需要致电requestAlwaysAuthorization()requestWhenInUseAuthorization()。这将触发对用户的警报。

如果您通过长按应用程序图标并点击X来删除应用程序,则会撤消用户使用位置服务的权限,因此在您致电{{{}之前,该应用将被拒绝访问再次1}}或requestAlwaysAuthorization()