iOS應(yīng)用獲取相冊及相機(jī)權(quán)限

一、判斷用戶訪問相冊權(quán)限
在iOS應(yīng)用中如果調(diào)用系統(tǒng)相冊或相機(jī),需要用戶具有相應(yīng)的權(quán)限,才可進(jìn)行一定的操作。那在應(yīng)用中如何獲取當(dāng)前相冊及相機(jī)的權(quán)限呢,有如下兩種方式:其實(shí)兩種方式基本一樣,只是Apple 更改了API調(diào)用方式。
1、iOS 8.0以下系統(tǒng)
首先,導(dǎo)入需要的頭文件以及AssetsLibrary框架。

#import <AssetsLibrary/AssetsLibrary.h>
然后即可獲取權(quán)限
     ALAuthorizationStatus author = [ALAssetsLibraryauthorizationStatus];
    if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){
        //無權(quán)限
    }if  … 其他情況依次判斷
     所有權(quán)限狀態(tài)如下:
    typedef enum {
        kCLAuthorizationStatusNotDetermined = 0, // 用戶尚未做出選擇這個(gè)應(yīng)用程序的問候
        kCLAuthorizationStatusRestricted,        // 此應(yīng)用程序沒有被授權(quán)訪問的照片數(shù)據(jù)??赡苁羌议L控制權(quán)限
        kCLAuthorizationStatusDenied,            // 用戶已經(jīng)明確否認(rèn)了這一照片數(shù)據(jù)的應(yīng)用程序訪問
        kCLAuthorizationStatusAuthorized         // 用戶已經(jīng)授權(quán)應(yīng)用訪問照片數(shù)據(jù)
    }

2、iOS 8.0以上系統(tǒng)
首先,導(dǎo)入需要的頭文件以及Photos框架。

#import <Photos/Photos.h>
然后即可獲取權(quán)限
     PHAuthorizationStatus authorStatus = [PHPhotoLibrary authorizationStatus];
     NSLog(@"openGallery_authorStatus == %ld",(long)authorStatus);
     if (authorStatus == PHAuthorizationStatusAuthorized){
     //獲取權(quán)限
     }if  … 其他情況依次判斷
     所有權(quán)限狀態(tài)如下,跟上面一樣,只是接口名稱不同。
      typedefNS_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);

3、iOS10以上系統(tǒng)
首先,需在工程對應(yīng)的plist文件內(nèi)添加“Privacy - Photo Library Usage Description”這個(gè)key,同時(shí)設(shè)置其值為“App needs your permission to access the Photo”類似這樣的說明。

  //獲取相冊訪問權(quán)限
    PHAuthorizationStatus photoStatus = [PHPhotoLibrary authorizationStatus];
    
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            switch (status) {
                case PHAuthorizationStatusAuthorized: //已獲取權(quán)限
                    break;
                
                case PHAuthorizationStatusDenied: //用戶已經(jīng)明確否認(rèn)了這一照片數(shù)據(jù)的應(yīng)用程序訪問
                    break;
                
                case PHAuthorizationStatusRestricted://此應(yīng)用程序沒有被授權(quán)訪問的照片數(shù)據(jù)??赡苁羌议L控制權(quán)限
                    break;
               
                default://其他。。。
                    break;
            }
        });
    }];

二、判斷用戶訪問相機(jī)權(quán)限
iOS7之前都可以訪問相機(jī),iOS7之后訪問相機(jī)有權(quán)限設(shè)置
對相機(jī)的訪問權(quán)限獲取與相冊類似,直接上代碼
首先,導(dǎo)入需要的頭文件以及AVFoundation框架。

#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
然后即可獲取權(quán)限
     AVAuthorizationStatus status = [AVCaptureDeviceauthorizationStatusForMediaType:AVMediaTypeVideo];
     NSLog(@"openCamera_status == %ld",(longs)status);
    if (authorStatus == AVAuthorizationStatusAuthorized){
    //獲取權(quán)限

    }if  … 其他情況依次判斷
所有權(quán)限狀態(tài)如下,跟上面一樣,只是接口名稱不同。
typedefNS_ENUM(NSInteger, AVAuthorizationStatus) {
AVAuthorizationStatusNotDetermined = 0,
AVAuthorizationStatusRestricted, 
AVAuthorizationStatusDenied, 
AVAuthorizationStatusAuthorized
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容