前期设置
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中的设置如下图
这里写图片描述
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;
}
}
2.获取麦克风权限
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
NSLog(@"%@",granted ? @"麦克风准许":@"麦克风不准许");
}];
3.检测相机权限
- (void) checkVideoStatus
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (authStatus) {
case AVAuthorizationStatusNotDetermined:
//没有询问是否开启相机
self.videoStatus = @"AVAuthorizationStatusNotDetermined";
break;
case AVAuthorizationStatusRestricted:
//未授权,家长限制
self.videoStatus = @"AVAuthorizationStatusRestricted";
break;
case AVAuthorizationStatusDenied:
//未授权
self.videoStatus = @"AVAuthorizationStatusDenied";
break;
case AVAuthorizationStatusAuthorized:
//玩家授权
self.videoStatus = @"AVAuthorizationStatusAuthorized";
break;
default:
break;
}
}
4.获取相机权限
//授权相机
- (void)videoAuthAction
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
NSLog(@"%@",granted ? @"相机准许":@"相机不准许");
}];
}
5.检测照片权限
//检查照片权限
- (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;
}
}
6.获取照片权限
//授权照片
- (void)phontLibraryAction{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
}];
}