【问题标题】:Issue with access camera after denied the camera拒绝摄像头后访问摄像头的问题
【发布时间】:2017-07-26 20:03:09
【问题描述】:

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

【问题讨论】:

  • 再次请求许可
  • 我试过了,但我无法访问相机和照片库,你能提供一段代码吗?
  • @PatelJigar 我认为当用户第一次不允许时,不可能请求两次或更多次的许可

标签: ios swift camera native


【解决方案1】:

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

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

【讨论】:

  • 谢谢你,杰克,非常感谢,让我们检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
相关资源
最近更新 更多