iOS10判斷攝像機(jī)是否可用

97G58PICw6n_1024.jpg

自言自語:(最近想買一直比較忙,都很少有時(shí)間寫點(diǎn)東西和大家分享,要堅(jiān)持寫點(diǎn)東西啊!)

iOS10以后增加了權(quán)限的管理,更注重用戶的隱私

我們?cè)陂_發(fā)的時(shí)候避免不了會(huì)調(diào)用用戶的相機(jī)或者相冊(cè),在iOS10以后就需要在info.plist文件里添加這兩個(gè)key Privacy - Camera Usage DescriptionPrivacy - Photo Library Usage Description 分別對(duì)應(yīng):相機(jī)權(quán)限相冊(cè)權(quán)限,具體的key查看這篇文章ios10要添加的key
雖然添加了這個(gè)key用戶在使用我們app的時(shí)候,當(dāng)初次使用到相機(jī)的時(shí)候,如果用戶不允許,那么下次在使用的時(shí)候就不會(huì)在提示,如果你不做判斷還繼續(xù)使用你的相機(jī),很有可能造成崩潰的bug(別覺得我說的很嚴(yán)重,我做的二維碼掃描就遇到這樣的問題了)

廢話不多說,上代碼

在需要掉攝像頭的地方

  /// 先判斷攝像頭硬件是否好用
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    /// 用戶是否允許攝像頭使用
    NSString * mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus  authorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
  /// 不允許彈出提示框
    if (authorizationStatus == AVAuthorizationStatusRestricted|| authorizationStatus == AVAuthorizationStatusDenied) {
        
        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"攝像頭訪問受限" message:nil preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:nil];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertC addAction:action];
    }else{
  ////這里是攝像頭可以使用的處理邏輯
}   
} else {
  /// 硬件問題提示
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"手機(jī)攝像頭設(shè)備損壞" message:@"" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
    
    [alertView show];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容