美文网首页
iOS 相册、麦克风和相机权限逻辑完善

iOS 相册、麦克风和相机权限逻辑完善

作者: 那已逝的时光 | 来源:发表于2019-11-22 10:00 被阅读0次

    因为相机是相册是日常开发中必不可少的功能,但是因为苹果的隐私政策,每次访问的时候都需要授权,没有授权app是无法访问相册和相机功能的,这样会造成app在用户第一次使用的时候出现一下显示不正常或者没有响应的感觉。
    为了能提高用户的体验度,写一下相机相册简单的权限逻辑判断。
    1、相册权限:
    苹果文档中,相册权限状态有一下情况:
    iOS 8.0+

    API_AVAILABLE_BEGIN(macos(10.13), ios(8), tvos(10))
    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.  
    };
    

    通过一下方法判断是否继续下一步
    需要导入#import <Photos/Photos.h>

    
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
     if (status == PHAuthorizationStatusRestricted ||
                    status == PHAuthorizationStatusDenied) 
    {
                    //无权限
              [SVProgressHUD showErrorWithStatus:@"Please set the Allow APP to access your photo album. Settings > Privacy > Album"];
       return ;
    }
    //第一次进来还没有选择是否授权
    else if (status == PHAuthorizationStatusNotDetermined)
     {
                    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied)
                            {
                                // 用户拒绝,跳转到自定义提示页面
                                
                            }
                            else if (status == PHAuthorizationStatusAuthorized)
                            {
                                // 用户授权,弹出相册对话框
                                
                            }
                        });
                    }];
                    return;
    }
    else{
        // 用户授权,弹出相册对话框
    }
    

    2、相机
    同相册一样,相机也是有对应的权限状态

    typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
        AVAuthorizationStatusNotDetermined = 0,//没有选择
        AVAuthorizationStatusRestricted    = 1,//已拒绝
        AVAuthorizationStatusDenied        = 2,//已拒绝
        AVAuthorizationStatusAuthorized    = 3,//已授权
    } API_AVAILABLE(macos(10.14), ios(7.0)) __WATCHOS_PROHIBITED __TVOS_PROHIBITED;
    

    导入#import <AVFoundation/AVCaptureDevice.h>

    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
                if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
                    //无权限 
                    
                    return ;
                }
                else if (authStatus == AVAuthorizationStatusNotDetermined)
                {
                    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                        if (!granted) {//不允许
                            
                        }else{//开启
                            
                        }
                    }];
                    return;
                }
    else
    {
      //已授权
    }
    
    

    获取麦克风权限逻辑和相机是差不多的只需把AVMediaTypeVideo 替换成AVMediaTypeAudio就可以了。
    这就是目前相机、相册和麦克风的权限逻辑了,需要注意的一点是,这些方法大多是异步的,所以如果要在返回后做对应的操作,最好切换到主线程。

    相关文章

      网友评论

          本文标题:iOS 相册、麦克风和相机权限逻辑完善

          本文链接:https://www.haomeiwen.com/subject/axdfwctx.html