我们经常使用到iOS访问相册、语音等一些权限,当用户使用到此功能时就给出弹出框,iOS默认第一次访问时会自动去请求系统权限。如果没有其他交互貌似可以符合我们的要求。但当我们有交互,比如点击录音我们自己有界面交互。
所以我们需要在点击时获取用户权限状态,然后再根据状态值去判断我们相应操作。是需要弹出用户允许权限,还是需要通知到设置中开启权限,还是已经允许直接给出我们代码交互逻辑。
1,在plist文件设置权限
Privacy - Microphone Usage Description:设置麦克风权限
Privacy - Photo Library Additions Usage Description:设置保存图片到相册权限
Privacy - Photo Library Usage Description:设置访问相册权限
Privacy - Camera Usage Description:打开相机权限
Privacy - Contacts Usage Description:设置通讯录权限
或者设置打开plist——> Source Code添加
`<string>需要拍照上传图片信息,是否允许打开相机?</string>
<key>NSFaceIDUsageDescription</key>
<string>需要您的同意才能访问Face ID</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>拍照会获取您的地理位置按地理信息分类照片</string>
<key>NSMicrophoneUsageDescription</key>
<string>请求访问麦克风</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>保存图片</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>需要添加图片,是否允许打开相册</string>
<key>NSContactsUsageDescription</key>
<string>访问通讯录</string>`
2,代码逻辑编写。
获取访问相册权限导入#import <Photos/Photos.h>
// 询问 相册权限
- (void)getPhotoLibrary:(void(^)(BOOL))result {
/// 获取当前的状态
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusAuthorized) {
result(YES);
return;
}
if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied) {
result(NO); //无权限
return ;
}
if (status == PHAuthorizationStatusNotDetermined) {
[self getPhotoLibrary];
;
}
}
- (void)getPhotoLibrary{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
NSLog(@"%@",[NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
});
}];
}
- (void)goset{
NSString *message = @"无法访问相册,请在设置中打开访问相册权限";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去设置", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) { // 去设置界面,开启相机访问权限
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
访问麦克风权限,导入#import<AVFoundation/AVFoundation.h>
- (void) checkAudioStatus{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (authStatus) {
case AVAuthorizationStatusNotDetermined:
[self getAVMediaTypeAudio];
break;
case AVAuthorizationStatusRestricted:
[self goset];
break;
case AVAuthorizationStatusDenied:
[self goset];
break;
case AVAuthorizationStatusAuthorized:
// 已授权做自己的事;
break;
default:
break;
}
}
- (void)getAVMediaTypeAudio{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
});
}];
}
- (void)goset{
NSString *message = @"无法录音,请在设置中打开麦克风访问权限";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去设置", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) { // 去设置界面,开启麦克风访问权限
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
获取通讯录权限:iOS9之后ContactsUI代替了AddressBookUI,Contacts代替了AddressBook。导入#import <ContactsUI/ContactsUI.h>
#pragma mark 请求通讯录权限
- (void)requestAuthorizationForAddressBook{
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusNotDetermined) { //用户没有设置
// 获取权限
[self getAddressBook];
}
else if(status == CNAuthorizationStatusRestricted ||status == CNAuthorizationStatusDenied){//弹出提示
[self goset];
}
else if (status == CNAuthorizationStatusAuthorized){//已经授权
//做自己的事情
}
}
- (void)getAddressBook{
//请求通讯录权限
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError* _Nullable error) {
if (error) {
NSLog(@"授权失败");
}else {
NSLog(@"成功授权");
}
}];
}
- (void)goset{
NSString *message = @"无法访问通讯录,请在设置中打开通讯录访问权限";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去设置", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) { // 去设置界面,开启通讯录访问权限
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
其他权限类似,就不一一写出来了。如果大家有什么更好的方式,欢迎留言,谢谢。