拒绝摄像头后访问摄像头出现问题

Pre*_*a P 2 camera native ios swift

在我的应用程序中,我使用了相机实现,因为如果用户首先输入意味着他们选择允许的时间意味着他们必须访问整个应用程序中的相机和照片库,如果选择拒绝意味着用户无法访问相机,所以那时我们需要在设置中更改相机和照片库。之后我们必须访问相机(在我的情况下它无法正常工作),任何人都可以帮助我,非常感谢。

Jac*_*ack 5

您可以先检查是否允许相机权限。如果没有,您可以导航到设置屏幕并请求用户许可。

 override func viewDidLoad() {
        super.viewDidLoad()
        checkCameraPermission()
    }
    func checkCameraPermission()  {
        let cameraMediaType = AVMediaTypeVideo
        AVCaptureDevice.requestAccess(forMediaType: cameraMediaType) { granted in
            if granted {
                //Do operation
                print("Granted access for camera")
                self.setCamera()
            } else {
                self.noCameraFound()
                print("Denied access for camera ")
            }
        }
    }
    func noCameraFound(){
        let alert = UIAlertController(title: "AppName", message: "Please allow camera access in phone settings", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.cancel, handler: {(action:UIAlertAction) in


        }));

        alert.addAction(UIAlertAction(title: "Open setting ", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
            UIApplication.shared.open(NSURL(string:UIApplicationOpenSettingsURLString)! as URL, options: [:], completionHandler: nil)

        }));
        self.present(alert, animated: true, completion: nil)
    }
Run Code Online (Sandbox Code Playgroud)