How to check if the user gave permission to use the camera?

前端 未结 6 2056
余生分开走
余生分开走 2020-12-12 14:34

Trying to write this:

if usergavepermissiontousercamera  
  opencamera
else 
  showmycustompermissionview

Couldn\'t find a current way to d

6条回答
  •  感情败类
    2020-12-12 14:55

    I have modified the above answer and removed the initial prompt, since when we want to use device's camera the system is prompting for permissions itself:

    func checkPermissions() {
        let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
    
        switch authStatus {
        case .authorized:
            setupCamera()
        case .denied:
            alertPromptToAllowCameraAccessViaSetting()
        default:
            // Not determined fill fall here - after first use, when is't neither authorized, nor denied
            // we try to use camera, because system will ask itself for camera permissions
            setupCamera()
        }
    }
    
    func alertPromptToAllowCameraAccessViaSetting() {
        let alert = UIAlertController(title: "Error", message: "Camera access required to...", preferredStyle: UIAlertControllerStyle.alert)
    
        alert.addAction(UIAlertAction(title: "Cancel", style: .default))
        alert.addAction(UIAlertAction(title: "Settings", style: .cancel) { (alert) -> Void in
            UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
        })
    
        present(alert, animated: true)
    }
    

提交回复
热议问题