iOS關(guān)于相機(jī)相冊(cè)權(quán)限設(shè)置

iOS10出來之后,有一些童鞋提審應(yīng)用時(shí)會(huì)出現(xiàn)因?yàn)闆]有對(duì)相機(jī)相冊(cè)等權(quán)限的設(shè)置提醒而被拒絕,以及出現(xiàn)調(diào)取本地相冊(cè)相機(jī)等出現(xiàn)崩潰,這是蘋果為了安全而設(shè)置的權(quán)限所導(dǎo)致的,解決的辦法就是在 plist 文件里添加相應(yīng)的獲取權(quán)限。

配置權(quán)限:
相機(jī)權(quán)限:Privacy - Camera Usage Description 允許此權(quán)限才能使用相機(jī)功,這樣才能錄制視頻,并且想要保存圖片。
相冊(cè)權(quán)限:Privacy - Photo Library Usage Description 允許此權(quán)限才能使用系統(tǒng)相冊(cè)。
麥克風(fēng)權(quán)限:Privacy - Microphone Usage Description 獲取麥克風(fēng)權(quán)限不然會(huì)崩,只有允許此權(quán)限才能錄音。
在info.plist里增加一項(xiàng),key從上面的三項(xiàng)任一項(xiàng)拷貝,然后運(yùn)行后會(huì)出現(xiàn)授權(quán)的警示框,同意后就沒有問題了。

<!-- 相冊(cè) -->   
<key>NSPhotoLibraryUsageDescription</key>   
<string>App需要您的同意,才能訪問相冊(cè)</string>   
<!-- 相機(jī) -->   
<key>NSCameraUsageDescription</key>   
<string>App需要您的同意,才能訪問相機(jī)</string>   
<!-- 麥克風(fēng) -->   
<key>NSMicrophoneUsageDescription</key>   
<string>App需要您的同意,才能訪問麥克風(fēng)</string>   
<!-- 位置 -->   
<key>NSLocationUsageDescription</key>   
<string>App需要您的同意,才能訪問位置</string>   
<!-- 在使用期間訪問位置 -->   
<key>NSLocationWhenInUseUsageDescription</key>   
<string>App需要您的同意,才能在使用期間訪問位置</string>   
<!-- 始終訪問位置 -->   
<key>NSLocationAlwaysUsageDescription</key>   
<string>App需要您的同意,才能始終訪問位置</string>   
<!-- 日歷 -->   
<key>NSCalendarsUsageDescription</key>   
<string>App需要您的同意,才能訪問日歷</string>   
<!-- 提醒事項(xiàng) -->   
<key>NSRemindersUsageDescription</key>   
<string>App需要您的同意,才能訪問提醒事項(xiàng)</string>   
<!-- 運(yùn)動(dòng)與健身 -->   
<key>NSMotionUsageDescription</key> 
<string>App需要您的同意,才能訪問運(yùn)動(dòng)與健身</string>   
<!-- 健康更新 -->   
<key>NSHealthUpdateUsageDescription</key>   
<string>App需要您的同意,才能訪問健康更新 </string>   
<!-- 健康分享 -->   
<key>NSHealthShareUsageDescription</key>   
<string>App需要您的同意,才能訪問健康分享</string>   
<!-- 藍(lán)牙 -->   
<key>NSBluetoothPeripheralUsageDescription</key>   
<string>App需要您的同意,才能訪問藍(lán)牙</string>   
<!-- 媒體資料庫 -->   
<key>NSAppleMusicUsageDescription</key>  
<string>App需要您的同意,才能訪問媒體資料庫</string>  

info.plist中逐個(gè)添加 KEY直接復(fù)制 value的string字符串就是提示的文字 可以根據(jù)自己需要填寫

one.png

判斷相機(jī)權(quán)限是否被限制,判斷相機(jī)是否可以使用

判斷相機(jī)權(quán)限是否被限制

需要導(dǎo)入 AVFoundation 類

import <AVFoundation/AVFoundation.h>

// iOS 判斷應(yīng)用是否有使用相機(jī)的權(quán)限

NSString *mediaType = AVMediaTypeVideo;//讀取媒體類型  
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//讀取設(shè)備授權(quán)狀態(tài)  
if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){  
    NSString *errorStr = @"應(yīng)用相機(jī)權(quán)限受限,請(qǐng)?jiān)谠O(shè)置中啟用";  
    [[HUDHelper getInstance] showErrorTipWithLabel:errorStr view:self.navigationController.view];  
    return;  
}  

如圖狀態(tài)是一個(gè)枚舉

typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {  
    AVAuthorizationStatusNotDetermined = 0,  
    AVAuthorizationStatusRestricted,  
    AVAuthorizationStatusDenied,  
    AVAuthorizationStatusAuthorized  
} NS_AVAILABLE_IOS(7_0);  

AVAuthorizationStatusNotDetermined  

用戶還沒有對(duì)應(yīng)用程序授權(quán)進(jìn)行操作

AVAuthorizationStatusRestricted  

還沒有授權(quán)訪問的照片數(shù)據(jù)。

AVAuthorizationStatusDenied  

用戶拒絕對(duì)應(yīng)用程序授權(quán)

AVAuthorizationStatusAuthorized  

用戶對(duì)應(yīng)用程序授權(quán)

另外,需要對(duì)相機(jī)進(jìn)行判斷是否被授權(quán),而相冊(cè)不需要判斷是否授權(quán)。
因?yàn)橄鄼C(jī)沒有授權(quán)的話不能被使用卻沒有任何有用的提示。

01E8E.png

可以自行根據(jù)判斷設(shè)置成這樣的提示

C95CA.png

而相冊(cè)的話,系統(tǒng)默認(rèn)modol出界面提示

7AD83.png

就不需要我們進(jìn)行判斷,提示用戶了。

上述視圖判斷邏輯代碼如下



- (void)PhotoClick:(UIButton *)button{
    
    switch (button.tag) {
        case 1:{
            DLog(@"拍照");
            [self.darkView removeFromSuperview];
            UIImagePickerController *pick = [[UIImagePickerController alloc]init];
            pick.sourceType = UIImagePickerControllerSourceTypeCamera;
            pick.delegate = self;
            
            
            //判斷是否有相機(jī)權(quán)限
            NSString *mediaType = AVMediaTypeVideo;//讀取媒體類型
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//讀取設(shè)備授權(quán)狀態(tài)
            if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
                NSString *errorStr = @"應(yīng)用相機(jī)權(quán)限受限,請(qǐng)?jiān)趇Phone的“設(shè)置-隱私-相機(jī)”選項(xiàng)中,允許好享玩訪問你的相機(jī)。";
                DLog(@"相機(jī)不可用");
                
                //必須使用present 方法
                //[self presentViewController:pick animated:YES completion:nil];
                
                [self showAlertControllerWithMessage:errorStr];
            
            
            } else {
            
                DLog(@"相機(jī)可用");
                
                //必須使用present 方法
                [self presentViewController:pick animated:YES completion:nil];
            
            }
            
            
            
        }
            
            break;
        case 2:{
            DLog(@"相冊(cè)");
            [self.darkView removeFromSuperview];
            UIImagePickerController *pick = [[UIImagePickerController alloc]init];
            pick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            pick.delegate = self;
            //必須使用present 方法調(diào)用相冊(cè)  相機(jī)也一樣
            [self presentViewController:pick animated:YES completion:nil];
            
        }
            break;
        default:
            break;
    }
}



判斷相機(jī)是否可以使用

以下是參考方法:

pragma mark - 攝像頭和相冊(cè)相關(guān)的公共類

// 判斷設(shè)備是否有攝像頭  
- (BOOL) isCameraAvailable{  
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];  
}  
  
// 前面的攝像頭是否可用  
- (BOOL) isFrontCameraAvailable{  
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];  
}  
  
// 后面的攝像頭是否可用  
- (BOOL) isRearCameraAvailable{  
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];  
}  

相應(yīng)的我們需要判斷用戶的攝像頭是否是壞的,以防程序crash

if (![self isFrontCameraAvailable]) {  
        //判斷相機(jī)是否可用  
        NSString *errorStr = @"相機(jī)出現(xiàn)問題,將跳轉(zhuǎn)到相冊(cè)選擇照片";  
        [[HUDHelper getInstance] showErrorTipWithLabel:errorStr view:self.navigationController.view];  
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{  
            [self openPhotoLibrary];  
        });  
        return;  
    }  

如果攝像頭壞了的話,我們可以直接跳到從相冊(cè)中選擇照片。

判斷用戶訪問相冊(cè)權(quán)限

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

//獲取相冊(cè)訪問權(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(zhǎng)控制權(quán)限
                break;
           
            default://其他。。。
                break;
        }
    });
}];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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