iOS关于相机相册权限设置

iOS10之后,有一些童鞋提审应用时会出现因为没有对相机相册等权限的设置提醒而被拒绝,以及出现调取本地相册相机等出现崩溃,这是苹果为了安全而设置的权限所导致的,解决的办法就是在 plist 文件里添加相应的获取权限。

配置权限:

相机权限:Privacy - Camera Usage Description 允许此权限才能使用相机功,这样才能录制视频,并且想要保存图片。

相册权限:Privacy - Photo Library Usage Description 允许此权限才能使用系统相册。

麦克风权限:Privacy - Microphone Usage Description 获取麦克风权限不然会崩,只有允许此权限才能录音。

在info.plist里增加一项,key从上面的三项任一项拷贝,然后运行后会出现授权的警示框,同意后就没有问题了。

<!-- 麦克风 --> 
<key>NSMicrophoneUsageDescription</key> 
<string>App需要您的同意,才能访问麦克风</string> 

<!-- 相册 --> 
<key>NSPhotoLibraryUsageDescription</key> 
<string>App需要您的同意,才能访问相册</string> 

<!-- 相机 --> 
<key>NSCameraUsageDescription</key> 
<string>App需要您的同意,才能访问相机</string> 

<!-- 通讯录 --> 
<key>NSContactsUsageDescription</key> 
<string>App需要您的同意,才能访问通讯录</string> 

<!-- 蓝牙 --> 
<key>NSBluetoothPeripheralUsageDescription</key> 
<string>App需要您的同意,才能使用蓝牙</string> 

<!-- 语音转文字 --> 
<key>NSSpeechRecognitionUsageDescription</key> 
<string>App需要您的同意,才能使用语音识别</string> 

<!-- 日历 --> 
<key>NSCalendarsUsageDescription</key> 
<string>App需要您的同意,才能访问日历</string> 

<!-- 位置 --> 
<key>NSLocationUsageDescription</key> 
<string>App需要您的同意,才能访问位置</string> 

<!-- 在使用期间访问位置 --> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string>App需要您的同意,才能在使用期间访问位置</string> 

<!-- 始终访问位置 --> 
<key>NSLocationAlwaysUsageDescription</key> 
<string>App需要您的同意,才能始终访问位置</string> 

<!-- 媒体资料库 --> 
<key>NSAppleMediaLibraryUsageDescription</key> 
<string>App需要您的同意,才能访问媒体资料库</string> 

<!-- 音乐权限 --> 
<key>NSAppleMusicUsageDescription</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> 

<!-- Siri使用 --> 
<key>NSSiriUsageDescription</key> 
<string>App需要您的同意,才能使用Siri</string> 

<!-- 电视供应商 --> 
<key>NSTVProviderUsageDescription</key> 
<string>App需要您的同意,才能使用电视供应商</string> 

<!-- 视频用户账号使用 --> 
<key>NSVideoSubscriberAccountUsageDescription</key> 
<string>App需要您的同意,才能访问视频用户账号使用权限</string> 

info.plist中逐个添加 KEY直接复制 value的string字符串就是提示的文字 可以根据自己需要填写

one.png

判断相机权限是否被限制,判断相机是否可以使用。

判断相机权限是否被限制。

需要导入 AVFoundation 类。

import <AVFoundation/AVFoundation.h>

// iOS 判断应用是否有使用相机的权限

NSString *mediaType = AVMediaTypeVideo;//读取媒体类型 
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态 
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){ 
    NSString *errorStr = @"应用相机权限受限,请在设置中启用"; 
    [[HUDHelper getInstance] showErrorTipWithLabel:errorStr view:self.navigationController.view]; 
    return; 
} 

如下代码块状态是一个枚举。


typedef NS_ENUM(NSInteger, AVAuthorizationStatus) { 
    AVAuthorizationStatusNotDetermined = 0, 
    AVAuthorizationStatusRestricted, 
    AVAuthorizationStatusDenied, 
    AVAuthorizationStatusAuthorized 
} NS_AVAILABLE_IOS(7_0); 


AVAuthorizationStatusNotDetermined 

用户还没有对应用程序授权进行操作。


AVAuthorizationStatusRestricted 

还没有授权访问的照片数据。


AVAuthorizationStatusDenied 

用户拒绝对应用程序授权。


AVAuthorizationStatusAuthorized 

用户对应用程序授权。

另外,需要对相机进行判断是否被授权,而相册不需要判断是否授权。

因为相机没有授权的话不能被使用却没有任何有用的提示。

01E8E.png

可以自行根据判断设置成这样的提示。

C95CA.png

而相册的话,系统默认modol出界面提示。

7AD83.png

就不需要我们进行判断,提示用户了。

上述视图判断逻辑代码如下:


- (void)PhotoClick:(UIButton *)button{
    switch (button.tag) {
        case 1:{
            DLog(@"拍照");
            [self.darkView removeFromSuperview];
            UIImagePickerController *pick = [[UIImagePickerController alloc]init];
            pick.sourceType = UIImagePickerControllerSourceTypeCamera;
            pick.delegate = self;
            //判断是否有相机权限
            NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
            if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
                NSString *errorStr = @"应用相机权限受限,请在iPhone的“设置-隐私-相机”选项中,允许好享玩访问你的相机。";
                DLog(@"相机不可用");
                //必须使用present 方法
                //[self presentViewController:pick animated:YES completion:nil];
                [self showAlertControllerWithMessage:errorStr];
            } else {
                DLog(@"相机可用");
                //必须使用present 方法
                [self presentViewController:pick animated:YES completion:nil];
            }
        }
            break;
        case 2:{
            DLog(@"相册");
            [self.darkView removeFromSuperview];
            UIImagePickerController *pick = [[UIImagePickerController alloc]init];
            pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            pick.delegate = self;
            //必须使用present 方法调用相册  相机也一样
            [self presentViewController:pick animated:YES completion:nil];
        }
            break;
        default:
            break;
    }
}

判断相机是否可以使用。

以下是参考方法:

pragma mark - 摄像头和相册相关的公共类


// 判断设备是否有摄像头 
- (BOOL) isCameraAvailable{ 
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; 
} 

// 前面的摄像头是否可用 
- (BOOL) isFrontCameraAvailable{ 
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]; 
} 

// 后面的摄像头是否可用 
- (BOOL) isRearCameraAvailable{ 
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]; 
} 

相应的我们需要判断用户的摄像头是否是坏的,以防程序crash。


if (![self isFrontCameraAvailable]) { 
        //判断相机是否可用 
        NSString *errorStr = @"相机出现问题,将跳转到相册选择照片"; 
        [[HUDHelper getInstance] showErrorTipWithLabel:errorStr view:self.navigationController.view]; 
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
            [self openPhotoLibrary]; 
        }); 
        return; 
    } 

如果摄像头坏了的话,我们可以直接跳到从相册中选择照片。
判断用户访问相册权限。
iOS10以上系统。
首先,需在工程对应的plist文件内添加“Privacy - Photo Library Usage Description”这个key,同时设置其值为“App needs your permission to access the Photo”类似这样的说明。

//获取相册访问权限
    PHAuthorizationStatus photoStatus = [PHPhotoLibrary authorizationStatus];
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            switch (status) {
                case PHAuthorizationStatusAuthorized: //已获取权限
                    break;
                case PHAuthorizationStatusDenied: //用户已经明确否认了这一照片数据的应用程序访问
                    break;
                case PHAuthorizationStatusRestricted://此应用程序没有被授权访问的照片数据。可能是家长控制权限
                    break;
                default://其他。。。
                    break;
            }
        });
    }];

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

友情链接更多精彩内容