IOS 获取摄像头/麦克风/相册 权限

1. 获取摄像头/麦克风权限

#import  

//类型定义
typedef NSString * AVMediaType NS_EXTENSIBLE_STRING_ENUM;

AVF_EXPORT AVMediaType const AVMediaTypeVideo                 NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT AVMediaType const AVMediaTypeAudio                 NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT AVMediaType const AVMediaTypeText                  NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT AVMediaType const AVMediaTypeClosedCaption         NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT AVMediaType const AVMediaTypeSubtitle              NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT AVMediaType const AVMediaTypeTimecode              NS_AVAILABLE(10_7, 4_0);
AVF_EXPORT AVMediaType const AVMediaTypeMetadata              NS_AVAILABLE(10_8, 6_0);
AVF_EXPORT AVMediaType const AVMediaTypeMuxed                 NS_AVAILABLE(10_7, 4_0);

1.1 检测当前的权限状态

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
//权限定义
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
    AVAuthorizationStatusNotDetermined = 0, /* 没有询问是否开启麦克风*/
    AVAuthorizationStatusRestricted    = 1, /* 未授权,家长限制*/
    AVAuthorizationStatusDenied        = 2, /* 玩家未授权*/
    AVAuthorizationStatusAuthorized    = 3, /* 玩家授权*/
} API_AVAILABLE(macos(10.14), ios(7.0)) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;

如果要获取摄像头的权限,则将mediaType改为 AVMediaTypeVideo 即可。

1.2 请求麦克风权限

if (authStatus == AVAuthorizationStatusNotDetermined) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio
                                 completionHandler:^(BOOL granted) {
                                     dispatch_async(dispatch_get_main_queue(), ^{
                                         if (granted) {
                                             [self doSomething];
                                         } else {
                                             [self showAlertWithTitle:@"温馨提示" message:@"您没有开启\"麦克风\"权限\n 无法进行通话。请在设置中开启麦克风权限。"];
                                         }
                                     });
                                 }];
    } else if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
        [self showAlertWithTitle:@"温馨提示" message:@"您没有开启\"麦克风\"权限\n 无法进行通话。\n 请在设置中开启麦克风权限。"];
    } else if (authStatus == AVAuthorizationStatusAuthorized) {
         [self doSomething];
    }

如果要获取摄像头的权限,则将mediaType改为 AVMediaTypeVideo 即可。

2. 获取相册权限

OS8之后加入了PhotoKit,使用需要

 #import 

//权限类型
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
PHAuthorizationStatusNotDetermined = 0, /* 未询问用户是否授权*/
PHAuthorizationStatusRestricted, /* 未授权,例如家长控制*/
PHAuthorizationStatusDenied, /* 未授权,用户拒绝造成的*/
PHAuthorizationStatusAuthorized /* 已授权*/
}

2.1 检测权限

PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];

2.2 请求授权

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
// 用户同意授权
}else {
// 用户拒绝授权
}

3. 版本兼容

IOS10 需要在plist文件中添加获取权限声明,否则会崩溃,提示信息如下:

[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 NSMicrophoneUsageDescription key with a string value explaining to the user how the app uses this data.

plist中的设置如下所示:


权限设置.png

4. 后台处理

如果后台依旧需要采集声音,则需要设置后台,如下所示:


后台设置.png

你可能感兴趣的:(IOS 获取摄像头/麦克风/相册 权限)