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

前端 未结 6 2055
余生分开走
余生分开走 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 15:03

    You can use the following code for doing the same:

    if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) ==  AVAuthorizationStatus.Authorized {
        // Already Authorized
    } else {
        AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
           if granted == true {
               // User granted
           } else {
               // User rejected
           }
       })
    }
    

    NOTE:

    1. Make sure that you add the AVFoundation Framework in the Link Binary section of build phases
    2. You should write import AVFoundation on your class for importing AVFoundation

    SWIFT 3

    if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) ==  AVAuthorizationStatus.authorized {
       // Already Authorized
    } else {
       AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
          if granted == true {
             // User granted
          } else {
             // User Rejected
          }
       })
    }
    

    Swift 4

    if AVCaptureDevice.authorizationStatus(for: .video) ==  .authorized {
        //already authorized
    } else {
        AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in
            if granted {
                //access allowed
            } else {
                //access denied
            }
        })
    }
    

提交回复
热议问题