// 判断授权状态
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];
}
}
网友评论