iOS11 访问相册、相机权限,居然变化了,巨坑啊.....

奋斗的七月
最近在做一个环信即时聊天的项目,突然发现了一个问题,在进行聊天时候,选择图片,居然没有询问用户,直接可以访问相册,当选择了一张图片的时候,这时候才会弹出询问用户的提示框。

这个问题纠结我好久!!!最后发现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.
 */

参考:
https://zhuanlan.zhihu.com/p/21526810

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在更新iOS11之后,访问相册出现crash现象,大家都知道访问相册需要申请用户权限。 相册权限需要在info.p...
    肉肉要次肉阅读 23,330评论 3 14
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,176评论 25 709
  • 大家都知道访问相册需要申请用户权限。 相册权限需要在info.plist—Property List文件中添加NS...
    小孩仔阅读 8,957评论 0 1
  • 自已看不惯哪些人,有赶脚自己有学文,不说脏字,用图片骂人。 真心的,用图片是说明我心目中的两个人。
    山谷里的人js阅读 1,599评论 0 0
  • 异星传奇·第六章 约德里大人作者/波罗的海 接下来会不会成为小白鼠,成为实验台上解剖的对象?对方是外星人,那么对于...
    波罗的海v阅读 4,028评论 14 3