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

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时没有去获取权限而直接去设置界面去开启权限找不到权限而做的笔记 希望对你们有用不要重复踩坑

文章参考 林希品的 获取相机权限 和 打开相机权限
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容