iOS 手机权限设置

2018-11-16  本文已影响0人  子懿黎昕

手机权限设置

在app中需要访问手机相册,相机,定位,通讯录等隐私数据的时候,如果不做权限设置app会crash。崩溃日志如下:

This app has crashed because it attempted to access privacy-sensitive data without a usage description.The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.

这是因为苹果加强了用户隐私的安全保护,在获取这些隐私数据的时候需要用户的授权。要解决这个问题,需要在info.plist中添加NSContactUsageDescription的key,然后填写对应的value。下面列举了对应的key(Source Code模式下):

<key>NSCameraUsageDescription</key>
<string>用于身份证验证服务</string>                             
<key>NSContactsUsageDescription</key>                                                                       
<string>用于填写紧急联系人信息</string>    
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>     
<string>定位用于帮助判断申请贷款资格的审核,申请贷款时需要提交用户的位置信息</string>
<key>NSLocationAlwaysUsageDescription</key>         
<string>定位用于帮助判断申请贷款资格的审核,申请贷款时需要提交用户的位置信息</string>
<key>NSLocationWhenInUseUsageDescription</key>                             
<string>定位用于帮助判断申请贷款资格的审核,申请贷款时需要提交用户的位置信息</string> 
<key>NSPhotoLibraryAddUsageDescription</key>
<string>同意“App”访问您的相机,用于保存二维码图片</string>    
<key>NSPhotoLibraryUsageDescription</key>
<string>同意“APP”访问您的相机,用于保存二维码图片</string>
<key>NSMicrophoneUsageDescription</key>
<string>同意“APP”访问您的麦克风,用于语音通讯</string>
<key>NSCalendarsUsageDescription</key>
<string>同意“APP”访问您的日历,用于查看一段时间内的账户流水信</string>
<key>NSRemindersUsageDescription</key> 
<string>同意“APP”访问您的提醒事项,用于提醒您近期要处理的事情</string>
<key>NSMotionUsageDescription</key>
<string>同意“APP”访问您的运动与健身,用于对您每日步数的统计</string>
<key>NSHealthUpdateUsageDescription</key>
<string>同意“APP”访问您的健康,用于对您每日步数的统计</string>
<key>NSHealthShareUsageDescription</key>
<string>同意“APP”访问您的健康,用于对您每日步数的统计</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>同意“APP”访问您的蓝牙,用于对自行车开锁</string>
<key>NSAppleMusicUsageDescription</key>
<string>同意“APP”访问媒体资料库,用于XXX</string>

具体的描述要根据应用的功能进行填写!具体的描述要根据应用的功能进行填写!具体的描述要根据应用的功能进行填写!重要的事情说三遍。

如果用户在使用过程中没有开启响应的权限,可以提醒用户去开启,具体代码如下:

UIAlertController *alert = [UIAlertController
                                    alertControllerWithTitle:@"提示"
                                    message:@"请在iPhone的\"设置-隐私-"
                                    @"定位服务\"选项中,"
                                    @"允许本程序访问您的位置"
                                    preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction
                          actionWithTitle:@"确定"
                          style:UIAlertActionStyleDestructive
                          handler:^(UIAlertAction *_Nonnull action) {
                              
                              if ([[UIApplication sharedApplication]
                                   canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
                                  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                              } else {
                                  QDLog(@"error");
                              }
                          }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }]];
        [self presentViewController:alert animated:YES completion:nil];

关于ATS

在iOS 9中,默认http的网络请求是禁止的,如果在项目测试阶段需要访问http请求可以在'info.plist'文件中添加'NSAppTransportSecurity'字典,将'NSAllowsArbitraryLoads'设置为'YES'来禁用ATS。
具体设置如下:

//在项目的info.plist中
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

为了能够用户数据的隐私和通过苹果应用审核最好是采用https进行数据的访问。

上一篇 下一篇

猜你喜欢

热点阅读