iOS 相冊、麥克風和相機權限邏輯完善

因為相機是相冊是日常開發中必不可少的功能,但是因為蘋果的隱私政策,每次訪問的時候都需要授權,沒有授權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就可以了。
這就是目前相機、相冊和麥克風的權限邏輯了,需要注意的一點是,這些方法大多是異步的,所以如果要在返回后做對應的操作,最好切換到主線程。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容