iOS10适配 完美解决相机、相册等权限的使用

解决相机相册调用奔溃:

崩溃:[access] 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.

ios 10 中权限适配
升级到iOS10之后,需要设置权限的有:

   
<key>NSPhotoLibraryUsageDescriptionkey>   
<string>App需要您的同意,才能访问相册string>   
   
<key>NSCameraUsageDescriptionkey>   
<string>App需要您的同意,才能访问相机string>   
   
<key>NSMicrophoneUsageDescriptionkey>   
<string>App需要您的同意,才能访问麦克风string>   
   
<key>NSLocationUsageDescriptionkey>   
<string>App需要您的同意,才能访问位置string>   
   
<key>NSLocationWhenInUseUsageDescriptionkey>   
<string>App需要您的同意,才能在使用期间访问位置string>   
   
<key>NSLocationAlwaysUsageDescriptionkey>   
<string>App需要您的同意,才能始终访问位置string>   
   
<key>NSCalendarsUsageDescriptionkey>   
<string>App需要您的同意,才能访问日历string>   
   
<key>NSRemindersUsageDescriptionkey>   
<string>App需要您的同意,才能访问提醒事项string>   
   
<key>NSMotionUsageDescriptionkey> <string>App需要您的同意,才能访问运动与健身string>   
   
<key>NSHealthUpdateUsageDescriptionkey>   
<string>App需要您的同意,才能访问健康更新 string>   
   
<key>NSHealthShareUsageDescriptionkey>   
<string>App需要您的同意,才能访问健康分享string>   
   
<key>NSBluetoothPeripheralUsageDescriptionkey>   
<string>App需要您的同意,才能访问蓝牙string>   
   
<key>NSAppleMusicUsageDescriptionkey> 
 <string>App需要您的同意,才能访问媒体资料库string>  

info.plist中根据自己的需求复制添加
这里写图片描述

在使用时 .m 中添加

//相机
#import 
#import 
//相册
#import 

代码示例:
switch (buttonIndex) {
        case 1:
        {
            //相机权限
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
            if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。

                authStatus ==AVAuthorizationStatusDenied)  //用户已经明确否认了这一照片数据的应用程序访问
            {
                // 无权限 引导去开启
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication]canOpenURL:url]) {
                    [[UIApplication sharedApplication]openURL:url];
                }
            }else{
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                    [self loadImage:UIImagePickerControllerSourceTypeCamera];
                }
                else
                {
                    NSLog(@"手机不支持相机");
                }
            }
                    }
            break;
        case 2:
        {
            //相册权限
            ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
            if (author ==ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied){
                //无权限 引导去开启
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url];
                }
            }else{
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                    [self loadImage:UIImagePickerControllerSourceTypePhotoLibrary];
                }
                else
                {
                    NSLog(@"手机不支持相册");
                }
            }


        }
            break;

        default:
            break;
    }

你可能感兴趣的:(IOS10-适配)