内容简介:相册权限获取分三步:权限状态有下列四种:和相册权限一样,分三步:
苹果公司非常注重用户的隐私问题,所以在使用手机上一些涉及隐私的功能时,比如相册,相机,通讯录,录音权限等,需要征求用户的许可才可以使用。
一.权限状态说明
在使用相册,相机,通讯录,录音等功能时,我们需要通过不同的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
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
*/
+ (void)requestImagepickerAuthorization:(void(^)(LMAuthorizationStatus status))callback;
/**
请求相机权限
@param callback
*/
+ (void)requestCameraAuthorization:(void(^)(LMAuthorizationStatus status))callback;
/**
请求通讯录权限
@param callback
*/
+ (void)requestAddressBookAuthorization:(void(^)(LMAuthorizationStatus status))callback;
/**
请求录音权限
@param callback
*/
+ (void)requestRecordAuthorization:(void(^)(LMAuthorizationStatus))callback;
@end
NS_ASSUME_NONNULL_END
LMAuthorizationTool.m
#import "LMAuthorizationTool.h"
#import
#import
#import
#import
#import
@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
作者:雪山飞狐_91ae
链接:https://www.jianshu.com/p/c67f4bd69461
以上所述就是小编给大家介绍的《iOS相册,相机,通讯录,录音权限处理》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- iOS相册,相机,通讯录,录音权限处理
- python实现简易通讯录修改版
- 五分钟完成 ABP vNext 通讯录 App 开发
- 仿iPhone通讯录制作小程序自定义选择组件
- 纵横开阖-微信小程序之通讯录全攻略
- Java 开源办公开发平台 O2OA 新应用上线-通讯录
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTML5与CSS3基础教程(第7版)
[美] Elizabeth Castro、[美] Bruce Hyslop / 望以文 / 人民邮电出版社 / 2013-1 / 59.00元
代表下一代网页编写技术的HTML5,为网页提供布局和格式的CSS3,这两者构成了Web开发的基石,也是Web程序员和设计师必须熟练掌握的最基本技能。 本书是风靡全球的HTML和CSS最佳入门教程的最新版,上一版单单英文版的销量就超过100万册,被翻译为十多种语言,并长期雄踞亚马逊书店计算机图书排行榜榜首。 最新的第7版秉承前一版直观、透彻、全面、循序渐进的讲授特色,仍然采用独特的双栏图......一起来看看 《HTML5与CSS3基础教程(第7版)》 这本书的介绍吧!