IOS开发中的权限问题

参考http://mt.sohu.com/20160707/n458265498.shtml

对定位权限、定位是否开启的判断

// ios8.0+需要请求授权
if (isIOS(8.0)) {
    
    // 要在此处, 请求授权, 但是请求哪个权限, 没法确定, 靠其他开发者确定;
    
    // 1. 获取info.plist 文件内容
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    
    //            NSLog(@"%@", infoDic);
    
    // 2. 获取其他开发人员, 填写的key
    NSString *always = infoDic[@"NSLocationAlwaysUsageDescription"];
    NSString *whenInUse = infoDic[@"NSLocationWhenInUseUsageDescription"];
    
    if ([always length] > 0) {
        [_locationM requestAlwaysAuthorization];
    } else if ([whenInUse length] > 0) {
        [_locationM requestWhenInUseAuthorization];
        
        // 在前台定位授权状态下, 必须勾选后台模式location udpates才能获取用户位置信息
        NSArray *services = infoDic[@"UIBackgroundModes"];
        
        if (![services containsObject:@"location"]) {
            DBLog(@"友情提示: 当前状态是前台定位授权状态, 如果想要在后台获取用户位置信息, 必须勾选后台模式 location updates");
        } else {
            if (isIOS(9.0)) {
                _locationM.allowsBackgroundLocationUpdates = YES;
            }
        }
    } else {
        DBLog(@"错误---如果在iOS8.0之后定位, 必须在info.plist, 配置NSLocationWhenInUseUsageDescription 或者 NSLocationAlwaysUsageDescription");
    }
}

// 判断是否授权
CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];

if (authStatus == kCLAuthorizationStatusRestricted || authStatus == kCLAuthorizationStatusDenied) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"没有位置权限" message:@"请去设置-隐私-定位服务中对52度授权" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"现在就去" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"以后再说" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
}

// 定位服务未开启
if (![CLLocationManager locationServicesEnabled]) {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"打开“定位服务”来允许“52度”确定您的位置" message:@"52度将获取您的位置,为您提供更精确的服务" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

你可能感兴趣的:(IOS开发中的权限问题)