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];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 13,897评论 12 197
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,552评论 25 709
  • Down to lunch(下面简称DTL)最近很火,在app store美国区的社交榜排名前三,一度超过了fb,...
    cys的tony阅读 4,786评论 1 1
  • 江阴半岛北依福州,东南临海,与台湾隔峡相望。岛边有山,曰玉玺山,此山林木清秀,灵气十足。闲来登高,也是美事。 小岛...
    石竹阅读 2,586评论 35 10

友情链接更多精彩内容