手机通讯录权限:
/**
* 检测权限并作响应的操作
*/
- (void)checkAuthorizationStatus:(UISwitch *)sender {
switch (ABAddressBookGetAuthorizationStatus()) {
case kABAuthorizationStatusAuthorized: //存在权限
//获取通讯录
self.phonesAry = [self obtainContacts:self.addressBook];
if (_setAddBookBlock) {
_setAddBookBlock(sender.isOn ? 0 : 1, _phonesAry);
}
break;
case kABAuthorizationStatusNotDetermined: //权限未知
//请求权限
[self requestAuthorizationStatus:sender];
break;
case kABAuthorizationStatusDenied: //如果没有权限 需要提示
case kABAuthorizationStatusRestricted:
//弹窗提醒
{
NSString *appName = kApp_Display_Name;
UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"%@没有获取手机通讯录的权限",appName] message:[NSString stringWithFormat:@"请在“[设置]-[隐私]-[通讯录]”里允许%@使用",appName] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertview show];
//重置开关
dispatch_async(dispatch_get_main_queue(), ^{
[sender setOn:NO];
});
}
break;
default:
break;
}
}
/**
* 请求通讯录的权限
*/
- (void)requestAuthorizationStatus:(UISwitch *)sender {
WeakSelf(self);
ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error) {
//权限得到允许
if (granted == true) {
dispatch_async(dispatch_get_main_queue(), ^{
[sender setOn:YES];
weakSelf.phonesAry = [weakSelf obtainContacts:weakSelf.addressBook];
if (weakSelf.setAddBookBlock) {
weakSelf.setAddBookBlock(sender.isOn ? 0 : 1, weakSelf.phonesAry);
}
});
} else {
//不允许
dispatch_async(dispatch_get_main_queue(), ^{
[sender setOn:NO];
});
}
});
}
/**
* 获取通讯录中的联系人
*/
- (NSMutableArray *)obtainContacts:(ABAddressBookRef)addressBook {
//按照添加时间请求所有的联系人
CFArrayRef contants = ABAddressBookCopyArrayOfAllPeople(addressBook);
//存放所有联系人电话号码的数组
NSMutableArray *allNumArray = [NSMutableArray arrayWithCapacity:0];
//遍历获取所有的数据
for (NSInteger i = 0; i < CFArrayGetCount(contants); i++) {
//获得People对象
ABRecordRef recordRef = CFArrayGetValueAtIndex(contants, i);
NSArray *contact = [self contactPhonePropertyWithRecordRef:recordRef];
//添加对象
[allNumArray addObjectsFromArray:contact];
}
//释放资源
CFRelease(contants);
return allNumArray;
}
//获取单个联系人的电话号码数组
- (NSArray *)contactPhonePropertyWithRecordRef:(ABRecordRef)recordRef {
//外传数组
NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
//获得电话号码的多值对象
ABMultiValueRef values = ABRecordCopyValue(recordRef, kABPersonPhoneProperty);
for (NSInteger i = 0; i < ABMultiValueGetCount(values); i++) {
//电话号码
NSString *phoneNumber = (__bridge NSString *)ABMultiValueCopyValueAtIndex(values, i);
//添加数据
[phones addObject:phoneNumber];
}
//释放资源
CFRelease(values);
return [NSArray arrayWithArray:phones];
}
相机权限:
//检查相机权限
- (void)checkVideoStatus {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (authStatus) {
case AVAuthorizationStatusNotDetermined:
//没有询问是否开启相机
break;
case AVAuthorizationStatusRestricted:
//未授权,家长限制
break;
case AVAuthorizationStatusDenied:
//未授权
break;
case AVAuthorizationStatusAuthorized:
//玩家授权
break;
default:
break;
}
}
//授权相机
- (void)videoAuthAction {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
//相机准许
} else {
//相机不准许
}
}];
}
麦克风权限:
//检查麦克风权限
- (void)checkAudioStatus {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (authStatus) {
case AVAuthorizationStatusNotDetermined:
//没有询问是否开启麦克风
break;
case AVAuthorizationStatusRestricted:
//未授权,家长限制
break;
case AVAuthorizationStatusDenied:
//玩家未授权
break;
case AVAuthorizationStatusAuthorized:
//玩家授权
break;
default:
break;
}
}
//授权麦克风
- (void)audioAuthAction {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
if (granted) {
//麦克风准许
} else {
//麦克风不准许
}
}];
}
照片权限:
}
定位权限: