美文网首页
iOS问题 :需要获取相机权限的跳转判断

iOS问题 :需要获取相机权限的跳转判断

作者: ZJS_Sky | 来源:发表于2023-04-10 18:15 被阅读0次

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];
        });
}

总结:这些主要是之前判断状态为AVAuthorizationStatusNotDetermined时没有去获取权限而直接去设置界面去开启权限找不到权限而做的笔记 希望对你们有用不要重复踩坑

文章参考 林希品的 获取相机权限 和 打开相机权限

相关文章

网友评论

      本文标题:iOS问题 :需要获取相机权限的跳转判断

      本文链接:https://www.haomeiwen.com/subject/ezrwddtx.html