// 判斷授權狀態
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted) { // 此應用程序沒有被授權訪問的照片數據。可能是家長控制權限。
NSLog(@"因為系統原因, 無法訪問相冊");
} else if (status == PHAuthorizationStatusDenied) { // 用戶拒絕訪問相冊
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"請去-> [設置 - 隱私 - 相機 - 項目名稱] 打開訪問開關" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設置", nil];
[alertView show];
} else if (status == PHAuthorizationStatusAuthorized) { // 用戶允許訪問相冊
// 放一些使用相冊的代碼
} else if (status == PHAuthorizationStatusNotDetermined) { // 用戶還沒有做出選擇
// 彈框請求用戶授權
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) { // 用戶點擊了好
// 放一些使用相冊的代碼
}
}];
}
// 1、 獲取攝像設備
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
// 判斷授權狀態
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted) {
NSLog(@"因為系統原因, 無法訪問相機");
return;
} else if (authStatus == AVAuthorizationStatusDenied) { // 用戶拒絕當前應用訪問相機
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"請去-> [設置 - 隱私 - 相機 - 項目名稱] 打開訪問開關" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去設置", nil];
[alertView show];
return;
} else if (authStatus == AVAuthorizationStatusAuthorized) { // 用戶允許當前應用訪問相機
SGScanningQRCodeVC *scanningQRCodeVC = [[SGScanningQRCodeVC alloc] init];
scanningQRCodeVC.delegate = self;
[self presentViewController:scanningQRCodeVC animated:YES completion:nil];
} else if (authStatus == AVAuthorizationStatusNotDetermined) { // 用戶還沒有做出選擇
// 彈框請求用戶授權
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
// 用戶接受
SGScanningQRCodeVC *scanningQRCodeVC = [[SGScanningQRCodeVC alloc] init];
scanningQRCodeVC.delegate = self;
[self presentViewController:scanningQRCodeVC animated:YES completion:nil];
}
}];
}
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"未檢測到您的攝像頭, 請在真機上測試" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
[alertView show];
}
- UIAlertView的代理方法——跳轉到系統設置的隱私中開啟授權
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
// 系統是否大于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];
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。