1. 检测通讯录是否授权
+ (void)CheckAddressBookAuthorization:(void (^)(bool isAuthorized))block
{
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
if (authStatus != kABAuthorizationStatusAuthorized) {
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error){
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
NSLog(@"Error: %@", (__bridge NSError *)error);
}else if (!granted) {
block(NO);
}else {
block(YES);
}
});
});
}else {
block(YES);
}
}
2.是否开启相机权限
- (BOOL)isOpenCamera {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
return NO;
} else {
return YES;
}
}
注:
如果授权成功,则进行下一步操作,否则添加提示让用户去设置里面进行设置
进入用户设置的方法:
/**
* 跳转到系统设置页面,iOS8之后可用
*/
- (void)gotoSettings{
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]];
}
}
网友评论