iOS-问题:麦克风权限、相机权限、照片权限

一. plist文件设置

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中的设置如下图:

二. 权限

iOS设备的权限是在使用到相应权限的时候系统自动向用户获取权限,当然也可以自己通过代码向用户获取权限。

1. 麦克风权限

① 检测麦克风权限

//检查麦克风权限
- (void) checkAudioStatus{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
        //没有询问是否开启麦克风
        self.audioStatus = @"AVAuthorizationStatusNotDetermined";
        break;
        case AVAuthorizationStatusRestricted:
        //未授权,家长限制
        self.audioStatus = @"AVAuthorizationStatusRestricted";
        break;
        case AVAuthorizationStatusDenied:
        //用户拒绝
        self.audioStatus = @"AVAuthorizationStatusDenied";
        break;
        case AVAuthorizationStatusAuthorized:
        //用户同意
        self.audioStatus = @"AVAuthorizationStatusAuthorized";
        break;
        default:
        break;
    }
}

除了用户拒绝和家长限制未授权,其他都当用户同意处理,因为即便是未询问,系统也会自动询问用户。

② 获取麦克风权限

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
           NSLog(@"%@",granted ? @"麦克风准许":@"麦克风不准许");
       }];

调用上面的方法,系统会自动弹窗询问用户是否开启权限,如下:

如果用户选择“不允许”,上面的granted就是NO,表示用户拒绝了,我们在这个方法里面就可以检测用户是否同意给予权限了。

2. 相机权限

同上,只是将AVMediaTypeAudio换成AVMediaTypeVideo。

3. 照片权限

① 检测照片权限

//检查照片权限
- (void) checkPhotoStauts{
    PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
    switch (photoAuthorStatus) {
        case PHAuthorizationStatusAuthorized:
        self.photoLibraryStatus = @"PHAuthorizationStatusAuthorized";
        break;
        case PHAuthorizationStatusDenied:
        self.photoLibraryStatus = @"PHAuthorizationStatusDenied";
        break;
        case PHAuthorizationStatusNotDetermined:
        self.photoLibraryStatus = @"PHAuthorizationStatusNotDetermined";
        break;
        case PHAuthorizationStatusRestricted:
        self.photoLibraryStatus = @"PHAuthorizationStatusRestricted";
        break;
        default:
        break;
    }
   }

② 获取照片权限

//授权照片
- (void)phontLibraryAction{
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    }];
}

你可能感兴趣的:(iOS-问题:麦克风权限、相机权限、照片权限)