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

前端 未结 6 2035
余生分开走
余生分开走 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:53

    You can import the AVFoundation framework and use the authorizationStatus(for:) method shown below and handle the respective cases.

    switch AVCaptureDevice.authorizationStatus(for: .video) {
        case .authorized: // The user has previously granted access to the camera.
            self.setupCaptureSession()
    
        case .notDetermined: // The user has not yet been asked for camera access.
            AVCaptureDevice.requestAccess(for: .video) { granted in
                if granted {
                    self.setupCaptureSession()
                }
            }
    
        case .denied: // The user has previously denied access.
            return
        case .restricted: // The user can't grant access due to restrictions.
            return
    }
    

提交回复
热议问题