iOS相册,相机,通讯录,录音权限处理

栏目: IOS · 发布时间: 6年前

内容简介:相册权限获取分三步:权限状态有下列四种:和相册权限一样,分三步:

苹果公司非常注重用户的隐私问题,所以在使用手机上一些涉及隐私的功能时,比如相册,相机,通讯录,录音权限等,需要征求用户的许可才可以使用。

一.权限状态说明

在使用相册,相机,通讯录,录音等功能时,我们需要通过不同的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相册,相机,通讯录,录音权限处理》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

逻辑的引擎

逻辑的引擎

[美] 马丁·戴维斯 / 张卜天 / 湖南科学技术出版社 / 2005-5 / 20.00元

本书介绍了现代计算机背后的那些基本概念和发展这些概念的人,描写了莱布尼茨、布尔、费雷格、康托尔、希尔伯特、哥德尔、图灵等天才的生活和工作,讲述了数学家们如何在成果付诸应用之前很久就已经提出了其背后的思想。博达著作权代理有限公司授权出版据美国W.W.Norton公司2000年版本译出。2007年第二版亦使用同一ISBN。一起来看看 《逻辑的引擎》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具