苹果公司非常注重用户的隐私问题,所以在使用手机上一些涉及隐私的功能时,比如相册,相机,通讯录,录音权限等,需要征求用户的许可才可以使用。
一.权限状态说明
在使用相册,相机,通讯录,录音等功能时,我们需要通过不同的API获取到某一项功能的用户授权权限,用户授权的权限类型总结起来都可以归结为以下几种:
AuthorizationStatusNotDetermined // 用户从未进行过授权等处理,首次访问相应内容会提示用户进行授权
AuthorizationStatusAuthorized = 0, // 用户已授权,允许访问
AuthorizationStatusDenied, // 用户拒绝访问
AuthorizationStatusRestricted, //用户无法改编自合格权限,比如家长控制
二.具体的权限获取
1.相册权限获取
相册权限获取分三步:
- 设备是否支持
在获取相册的权限之前,先要判断设备是否支持获取媒体资源,比如我想要挑选照片,但是现在相册中照片数量为0,那这时候就不能支持相册了:
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]
typedef enum UIImagePickerControllerSourceType : NSInteger {
UIImagePickerControllerSourceTypePhotoLibrary, //相册
UIImagePickerControllerSourceTypeCamera, //相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum
} UIImagePickerControllerSourceType;
- 获取相册权限状态
如果第一步返回YES,也就是设备支持,那就可以获取目前的权限状态了:
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
权限状态有下列四种:
typedef enum PHAuthorizationStatus : NSInteger {
PHAuthorizationStatusNotDetermined = 0,
PHAuthorizationStatusRestricted,
PHAuthorizationStatusDenied,
PHAuthorizationStatusAuthorized
} PHAuthorizationStatus;
- 请求权限
第二步中若status为PHAuthorizationStatusNotDetermined,也就是还没有请求过权限,那么就要请求权限:
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
}];
2.相机权限获取
和相册权限一样,分三步:
- 设备是否支持:
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
- 获取相机权限状态
如果第一步返回YES,那么就可以获取目前的权限状态:
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
权限状态有下列四种:
typedef enum AVAuthorizationStatus : NSInteger {
AVAuthorizationStatusNotDetermined = 0,
AVAuthorizationStatusRestricted = 1,
AVAuthorizationStatusDenied = 2,
AVAuthorizationStatusAuthorized = 3
} AVAuthorizationStatus;
- 请求权限
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
}];
3.通讯录权限获取
- 获取通讯录权限状态:
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
权限状态有四种:
typedef enum CNAuthorizationStatus : NSInteger {
CNAuthorizationStatusNotDetermined = 0,
CNAuthorizationStatusRestricted,
CNAuthorizationStatusDenied,
CNAuthorizationStatusAuthorized
} CNAuthorizationStatus;
- 请求权限
CNContactStore *contact = [[CNContactStore alloc] init];
[contact requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
4.录音权限获取
- 获取录音权限状态:
AVAudioSession *session = [AVAudioSession sharedInstance];
AVAudioSessionRecordPermission permission = session.recordPermission;
权状态有三种:
typedef NS_ENUM(NSUInteger, AVAudioSessionRecordPermission)
{
AVAudioSessionRecordPermissionUndetermined = 'undt',
AVAudioSessionRecordPermissionDenied = 'deny',
AVAudioSessionRecordPermissionGranted = 'grnt'
};
- 请求权限
AVAudioSession *session = [AVAudioSession sharedInstance];
[session requestRecordPermission:^(BOOL granted) {
}];
三.用户拒绝授权后的处理
用户拒绝授权后,再下次要使用相应的功能,可能会忘记自己之前已经拒绝了该授权,从而可能获取不到数据,这个时候应该给用户一个提示,提示用户去设置里面更改权限。
这里的给出了一种做法就是直接跳转到该应用的设置页面:
NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:settingUrl]) {
[[UIApplication sharedApplication] openURL:settingUrl options:@{} completionHandler:^(BOOL success) {
}];
}
四.简单的封装
LMAuthorizationTool.h:
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, LMAuthorizationStatus) {
LMAuthorizationStatusAuthorized = 0, //用户已授权
LMAuthorizationStatusDenied, //拒绝
LMAuthorizationStatusRestricted, //应用没有相关权限,且当前用户无法改变这个权限,如家长控制
LMAuthorizationStatusNotSupport //硬件等不支持
};
typedef NS_ENUM(NSUInteger, LMMediaType) {
LMMediaTypeAlbum = 0, //相册
LMMediaTypeCamera, //相机
LMMediaTypeAddressBook //通讯录
};
@interface LMAuthorizationTool : NSObject
/**
请求相册访问权限
@param callback <callback description>
*/
+ (void)requestImagepickerAuthorization:(void(^)(LMAuthorizationStatus status))callback;
/**
请求相机权限
@param callback <callback description>
*/
+ (void)requestCameraAuthorization:(void(^)(LMAuthorizationStatus status))callback;
/**
请求通讯录权限
@param callback <callback description>
*/
+ (void)requestAddressBookAuthorization:(void(^)(LMAuthorizationStatus status))callback;
/**
请求录音权限
@param callback <callback description>
*/
+ (void)requestRecordAuthorization:(void(^)(LMAuthorizationStatus))callback;
@end
NS_ASSUME_NONNULL_END
LMAuthorizationTool.m
#import "LMAuthorizationTool.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <Contacts/Contacts.h>
@implementation LMAuthorizationTool
#pragma mark - 相册
+ (void)requestImagepickerAuthorization:(void (^)(LMAuthorizationStatus))callback
{
//检查设备是否支持
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] || [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
if (authStatus == PHAuthorizationStatusNotDetermined) { //未授权
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
[self executeCallback:callback status:LMAuthorizationStatusAuthorized];
} else if (status == PHAuthorizationStatusDenied) {
[self executeCallback:callback status:LMAuthorizationStatusDenied];
} else if (status == PHAuthorizationStatusRestricted) {
[self executeCallback:callback status:LMAuthorizationStatusRestricted];
}
}];
} else if (authStatus == PHAuthorizationStatusAuthorized) {
[self executeCallback:callback status:LMAuthorizationStatusAuthorized];
} else if (authStatus == PHAuthorizationStatusDenied) {
[self executeCallback:callback status:LMAuthorizationStatusDenied];
} else if (authStatus == PHAuthorizationStatusRestricted) {
[self executeCallback:callback status:LMAuthorizationStatusRestricted];
}
} else {
[self executeCallback:callback status:LMAuthorizationStatusNotSupport];
}
}
#pragma mark - 相机
+ (void)requestCameraAuthorization:(void (^)(LMAuthorizationStatus))callback
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusNotDetermined) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
[self executeCallback:callback status:LMAuthorizationStatusAuthorized];
} else {
[self executeCallback:callback status:LMAuthorizationStatusDenied];
}
}];
} else if (authStatus == AVAuthorizationStatusAuthorized) {
[self executeCallback:callback status:LMAuthorizationStatusAuthorized];
} else if (authStatus == AVAuthorizationStatusDenied) {
[self executeCallback:callback status:LMAuthorizationStatusDenied];
} else if (authStatus == AVAuthorizationStatusRestricted) {
[self executeCallback:callback status:LMAuthorizationStatusRestricted];
}
} else {
[self executeCallback:callback status:LMAuthorizationStatusNotSupport];
}
}
#pragma mark - 通讯录
+ (void)requestAddressBookAuthorization:(void (^)(LMAuthorizationStatus))callback
{
CNAuthorizationStatus authStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (authStatus == CNAuthorizationStatusNotDetermined) {
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
[self executeCallback:callback status:LMAuthorizationStatusAuthorized];
} else {
[self executeCallback:callback status:LMAuthorizationStatusDenied];
}
}];
} else if (authStatus == CNAuthorizationStatusAuthorized) {
[self executeCallback:callback status:LMAuthorizationStatusAuthorized];
} else if (authStatus == CNAuthorizationStatusDenied) {
[self executeCallback:callback status:LMAuthorizationStatusDenied];
} else if (authStatus == CNAuthorizationStatusRestricted) {
[self executeCallback:callback status:LMAuthorizationStatusRestricted];
}
}
#pragma mark - 录音
+ (void)requestRecordAuthorization:(void(^)(LMAuthorizationStatus))callback
{
AVAudioSession *session = [AVAudioSession sharedInstance];
AVAudioSessionRecordPermission permission = session.recordPermission;
if (permission == AVAudioSessionRecordPermissionUndetermined) {
[session requestRecordPermission:^(BOOL granted) {
if (granted) {
[self executeCallback:callback status:LMAuthorizationStatusAuthorized];
} else {
[self executeCallback:callback status:LMAuthorizationStatusDenied];
}
}];
} else if (permission == AVAudioSessionRecordPermissionGranted) {
[self executeCallback:callback status:LMAuthorizationStatusAuthorized];
} else if (permission == AVAudioSessionRecordPermissionDenied) {
[self executeCallback:callback status:LMAuthorizationStatusDenied];
}
}
#pragma mark - callback
+ (void)executeCallback:(void (^)(LMAuthorizationStatus))callback status:(LMAuthorizationStatus)status
{
dispatch_async(dispatch_get_main_queue(), ^{
if (callback) {
callback(status);
}
});
}
@end