// 判斷授權(quán)狀態(tài)
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted) { // 此應(yīng)用程序沒(méi)有被授權(quán)訪問(wèn)的照片數(shù)據(jù)。可能是家長(zhǎng)控制權(quán)限。
NSLog(@"因?yàn)橄到y(tǒng)原因, 無(wú)法訪問(wèn)相冊(cè)");
} else if (status == PHAuthorizationStatusDenied) { // 用戶(hù)拒絕訪問(wèn)相冊(cè)
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"請(qǐng)去-> [設(shè)置 - 隱私 - 相機(jī) - 項(xiàng)目名稱(chēng)] 打開(kāi)訪問(wèn)開(kāi)關(guān)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設(shè)置", nil];
[alertView show];
} else if (status == PHAuthorizationStatusAuthorized) { // 用戶(hù)允許訪問(wèn)相冊(cè)
// 放一些使用相冊(cè)的代碼
} else if (status == PHAuthorizationStatusNotDetermined) { // 用戶(hù)還沒(méi)有做出選擇
// 彈框請(qǐng)求用戶(hù)授權(quán)
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) { // 用戶(hù)點(diǎn)擊了好
// 放一些使用相冊(cè)的代碼
}
}];
}
// 1、 獲取攝像設(shè)備
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
// 判斷授權(quán)狀態(tài)
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted) {
NSLog(@"因?yàn)橄到y(tǒng)原因, 無(wú)法訪問(wèn)相機(jī)");
return;
} else if (authStatus == AVAuthorizationStatusDenied) { // 用戶(hù)拒絕當(dāng)前應(yīng)用訪問(wèn)相機(jī)
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"請(qǐng)去-> [設(shè)置 - 隱私 - 相機(jī) - 項(xiàng)目名稱(chēng)] 打開(kāi)訪問(wèn)開(kāi)關(guān)" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設(shè)置", nil];
[alertView show];
return;
} else if (authStatus == AVAuthorizationStatusAuthorized) { // 用戶(hù)允許當(dāng)前應(yīng)用訪問(wèn)相機(jī)
SGScanningQRCodeVC *scanningQRCodeVC = [[SGScanningQRCodeVC alloc] init];
scanningQRCodeVC.delegate = self;
[self presentViewController:scanningQRCodeVC animated:YES completion:nil];
} else if (authStatus == AVAuthorizationStatusNotDetermined) { // 用戶(hù)還沒(méi)有做出選擇
// 彈框請(qǐng)求用戶(hù)授權(quán)
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
// 用戶(hù)接受
SGScanningQRCodeVC *scanningQRCodeVC = [[SGScanningQRCodeVC alloc] init];
scanningQRCodeVC.delegate = self;
[self presentViewController:scanningQRCodeVC animated:YES completion:nil];
}
}];
}
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"未檢測(cè)到您的攝像頭, 請(qǐng)?jiān)谡鏅C(jī)上測(cè)試" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
[alertView show];
}
- UIAlertView的代理方法——跳轉(zhuǎn)到系統(tǒng)設(shè)置的隱私中開(kāi)啟授權(quán)
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// 系統(tǒng)是否大于10
NSURL *url = nil;
if ([[UIDevice currentDevice] systemVersion].floatValue < 10.0) {
url = [NSURL URLWithString:@"prefs:root=privacy"];
} else {
url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
}
[[UIApplication sharedApplication] openURL:url];
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。