最近在做一个环信即时聊天的项目,突然发现了一个问题,在进行聊天时候,选择图片,居然没有询问用户,直接可以访问相册,当选择了一张图片的时候,这时候才会弹出询问用户的提示框。
这个问题纠结我好久!!!最后发现iOS11,访问相册权限发生了重大变更:
一、 iOS11之前:访问相册和存储照片到相册(读写权限),需要用户授权,需要添加NSPhotoLibraryUsageDescription。
二、iOS11之后:默认开启访问相册权限(读权限),无需用户授权,无需添加NSPhotoLibraryUsageDescription,适配iOS11之前的还是需要加的。
添加图片到相册(写权限),需要用户授权,需要添加NSPhotoLibraryAddUsageDescription。
也就是说,ios11之后的系统,可以不需要进行询问用户,就可以直接访问相册。
但是这就出现了一个问题,可以不需要进行询问用户,但是选择图片之后,系统又会询问是否允许询问相册权限。这样又得用户还是不习惯这样的操作流程,还是希望当进入相册的时候,就询问用户,所以就需要代码进行控制弹框的顺序。
相册访问权限
在xcode8.0之前可以用ALAuthorizationStatus进行判断相册权限,在8.0系统以后,新加入了Photos.framework框架,我们可以利用框架中的PHAuthorizationStatus进行权限状态判断。
如何使用
#import <Photos/PHPhotoLibrary.h>
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied)
{
// 无权限
// do something...
}
各个状态的解释
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
PHAuthorizationStatusRestricted, // This application is not authorized to access photo data.
// The user cannot change this application’s status, possibly due to active restrictions
// such as parental controls being in place.
PHAuthorizationStatusDenied, // User has explicitly denied this application access to photos data.
PHAuthorizationStatusAuthorized // User has authorized this application to access photos data.
} NS_AVAILABLE_IOS(8_0);
以下是我项目中进行代码的代码判断,当第一次选择不允许的时候,第二次没有权限的时候,给一个优化的提示语:
//----第一次不会进来
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied){
// 无权限 做一个友好的提示
UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您设置允许该应用访问您的相机\n设置>隐私>相机" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alart show];
return;
}
//----每次都会走进来
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"Authorized");
}else{
NSLog(@"Denied or Restricted");
//----为什么没有在这个里面进行权限判断,因为会项目会蹦。。。
}
}];
相机访问权限
如何使用
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied)
{
// 无权限
// do something...
}
各个状态的解释
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
// 表明用户尚未选择关于客户端是否可以访问硬件
AVAuthorizationStatusNotDetermined = 0,
// 客户端未被授权访问硬件的媒体类型。用户不能改变客户机的状态,可能由于活跃的限制,如家长控制
AVAuthorizationStatusRestricted,
// 明确拒绝用户访问硬件支持的媒体类型的客户
AVAuthorizationStatusDenied,
// 客户端授权访问硬件支持的媒体类型
AVAuthorizationStatusAuthorized
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
/*!
@enum AVAuthorizationStatus
@abstract
Constants indicating the client's authorization to the underlying hardware supporting a media type.
@constant AVAuthorizationStatusNotDetermined
Indicates that the user has not yet made a choice regarding whether the client can access the hardware.
@constant AVAuthorizationStatusRestricted
The client is not authorized to access the hardware for the media type. The user cannot change
the client's status, possibly due to active restrictions such as parental controls being in place.
@constant AVAuthorizationStatusDenied
The user explicitly denied access to the hardware supporting a media type for the client.
@constant AVAuthorizationStatusAuthorized
The client is authorized to access the hardware supporting a media type.
*/