我是把这个方法写在BaseController 里面了
- (void)startScanQRCodeViewControllerWithResult:(void (^)(NSString * _Nonnull result))block {
// 1、 获取摄像设备
__weak typeof(self) weakSelf = self;
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (device) {
// 判断授权状态
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted) {
NSLog(@"因为系统原因, 无法访问相机");
return;
} else if (authStatus == AVAuthorizationStatusDenied) { // 用户拒绝当前应用访问相机
[self toSyeTomSetting:@"请点击'设置'去设置界面,允许微校wxiao访问你的相机。"];
return;
} else if (authStatus == AVAuthorizationStatusAuthorized) { // 用户允许当前应用访问相机
//允许访问相机
//do you work
} else if (authStatus == AVAuthorizationStatusNotDetermined) { // 用户还没有做出选择
// 弹框请求用户授权
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
//这里是在block里面操作UI,因此需要回到主线程里面去才能操作UI
dispatch_async(dispatch_get_main_queue(), ^{
//回到主线程里面就不会出现延时几秒之后才执行UI操作
//do you work
});
}else {
[WXOtherTool createSVPHUDWithString:@"你已取消授权" isError:YES];
}
}];
}
} else {
// [self toSyeTomSetting:@"未检测到您的摄像头, 请在真机上测试"];
}
}
- (void)toSyeTomSetting:(NSString *)message {
[WXOtherTool createSystemAlertControllerWithTitle:@"提示信息"
message:message
doneActionTitle:@"设置"
cancelActionTitle:@"取消"
showController:self
cancelAction:^(UIAlertAction * _Nonnull action) {}
doneAction:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
}
这就是相机权限请求时最坑的位子了,在block里面去执行了UI操作,所以导致了主线程的阻塞,所以我们这里需要回到主线程里面去操作UI
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
//这里是在block里面操作UI,因此需要回到主线程里面去才能操作UI
dispatch_async(dispatch_get_main_queue(), ^{
//回到主线程里面就不会出现延时几秒之后才执行UI操作
//do you work
});
}else {
[WXOtherTool createSVPHUDWithString:@"你已取消授权" isError:YES];
}
}];
网友评论