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 Description
和Privacy - 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];
}