1、需要导入头文件
#import <AVFoundation/AVFoundation.h>
2、跳转页面判断
-(void)gotoNextPage{
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus author = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if (author == AVAuthorizationStatusRestricted ||
author ==AVAuthorizationStatusDenied){
//无权限
DLog(@"暂无权限");
// [NSToastView nsShowToast:@"你还未打开相机权限,要扫码必须要先去设置打开相机权限"];
[self openDeviceSettingPage];
return;
}else if (author == AVAuthorizationStatusNotDetermined){
//获取权限 如果这个时候没有获取权限而是去设置界面在权限列表可能会没有相机的权限
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{
@strongify(self)
if (granted) {
//允许访问 如果明确下个界面可以在这边直接跳转
}else{
//不允许访问
}
});
}];
return;
}
if (TARGET_IPHONE_SIMULATOR) {
///屏蔽模拟器编译失败问题
return;
}
////这边就可以做跳转下个界面的操作了
}
///去项目的设置界面开启对应的权限
-(void)openDeviceSettingPage{
UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"打开摄像头失败" message:@"请在 设置-隐私-相机 开启权限" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertCtrl addAction:cancelAction];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}];
[alertCtrl addAction:okAction];
dispatch_async(dispatch_get_main_queue(), ^{
///[self currentNavigationController] 可以用你当前的控制器替换
[[self currentNavigationController] presentViewController:alertCtrl animated:YES completion:nil];
});
}