相机权限判断
/// 相机权限判断
-(void)getCameraPermissionBlock:(void(^)(BOOL isAuthorization))block{
NSInteger isAuthorization = 1;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (authStatus) {
case AVAuthorizationStatusNotDetermined:
//没有询问是否开启相机
isAuthorization = 0;
break;
case AVAuthorizationStatusRestricted:
//未授权,家长限制
isAuthorization = -1;
break;
case AVAuthorizationStatusDenied:
//用户未授权
isAuthorization = -1;
break;
case AVAuthorizationStatusAuthorized:
//用户授权
break;
default:
break;
}
if (isAuthorization == 0) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
block(granted);
}
});
}];
}else{
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
if (isAuthorization == 1) {
block(YES);
}else{
block(NO);
}
}
});
}
}
相册权限判断
// 相册权限判断
- (void)getPhotoAlbumPermissionBlock:(void(^)(BOOL isAuthorization))block{
NSInteger isAuthorization = 1;
PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
switch (photoAuthorStatus) {
case PHAuthorizationStatusNotDetermined:
//没有询问是否开启相机
isAuthorization = 0;
break;
case PHAuthorizationStatusRestricted:
//未授权,家长限制
isAuthorization = -1;
break;
case PHAuthorizationStatusDenied:
//用户未授权
isAuthorization = -1;
break;
case PHAuthorizationStatusLimited:
//用户限制性授权
break;
case PHAuthorizationStatusAuthorized:
//用户授权
break;
default:
break;
}
if (isAuthorization == 0) {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
if(status == PHAuthorizationStatusAuthorized || status == PHAuthorizationStatusLimited){
block(YES);
}else{
block(NO);
}
}
});
}];
}else{
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
if (isAuthorization == 1) {
block(YES);
}else{
block(NO);
}
}
});
}
}
麦克风权限判断
/// 麦克风权限判断
-(void)getMicrophonePermissionBlock:(void(^)(BOOL isAuthorization))block{
NSInteger isAuthorization = 1;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (authStatus) {
case AVAuthorizationStatusNotDetermined:
//没有询问是否开启麦克风
isAuthorization = 0;
break;
case AVAuthorizationStatusRestricted:
//未授权,家长限制
isAuthorization = -1;
break;
case AVAuthorizationStatusDenied:
//用户未授权
isAuthorization = -1;
break;
case AVAuthorizationStatusAuthorized:
//用户授权
break;
default:
break;
}
if (isAuthorization == 0) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
block(granted);
}
});
}];
}else{
dispatch_async(dispatch_get_main_queue(), ^{
if (block) {
if (isAuthorization == 1) {
block(YES);
}else{
block(NO);
}
}
});
}
}
通讯录权限判断
/// 通讯录权限判断
- (void)checkAddressBookAuthorization:(void (^)(bool isAuthorized))block{
CNContactStore *addressBook = [[CNContactStore alloc]init];
CNAuthorizationStatus authStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
switch (authStatus) {
case CNAuthorizationStatusAuthorized: //已授权
block(YES);
break;
case CNAuthorizationStatusNotDetermined: //授权未知
{
[addressBook requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error){
block(NO);
}else if (!granted){
block(NO);
}else{
block(YES);
}
});
}];
}
break;
default:
block(NO);
break;
}
}