#import <AssetsLibrary/AssetsLibrary.h>//判断拍照和相册是否授权需添加的
1.照片
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
if (author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied)
{
//无权限
//照片访问受限,请打开app访问你的照片! ;
return;
}
2.摄像
NSString * mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if (authorizationStatus == AVAuthorizationStatusRestricted|| authorizationStatus == AVAuthorizationStatusDenied) {
//摄像头访问受限,请打开摄像头允许拍摄!;
return;
}
3.麦克风
if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
[[AVAudioSession sharedInstance] performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
if (granted) {
//允许使用麦克风
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self initWithSetUpJumpAlert:@"无法使用麦克风,请设置打开麦克风!"];
return ;
});
}
}];
}
4.获取通讯录
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusNotDetermined) {
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
NSLog(@"weishouquan ");
}else
{
//用户给权限了
CNContactPickerViewController * picker = [CNContactPickerViewController new];
picker.delegate = self;
picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];//只显示手机号
[self presentViewController: picker animated:YES completion:nil];
}
}];
}
if (status == CNAuthorizationStatusAuthorized) {//有权限时
CNContactPickerViewController * picker = [CNContactPickerViewController new];
picker.delegate = self;
picker.displayedPropertyKeys = @[CNContactPhoneNumbersKey];
[self presentViewController: picker animated:YES completion:nil];
}
else{
//您未开启通讯录权限,请前往设置中心开启允许app访问您的通讯录
}
跳转到app设置对应的界面
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url])
{
NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}