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

查看所有标签

猜你喜欢:

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

大数据

大数据

涂子沛 / 广西师范大学出版社 / 2013-4-1 / 49.90元

公布官员财产美国是怎么做的,美国能让少部人腐败起来吗,美国式上访是怎么回事,凭什么美国矿难那么少,全民医改美国做得到吗,美国总统大选有什么利器才能赢,下一轮全球洗牌我们世界工厂会被淘汰吗…… 除了上帝,任何人都必须用数据来说话。 大数据浪潮,汹涌来袭,与互联网的发明一样,这绝不仅仅是信息技术领域的革命,更是在全球范围启动透明政府、加速企业创新、引领社会变革的利器。现代管理学之父德鲁克有......一起来看看 《大数据》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具