Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

Trying to write this:

if usergavepermissiontousercamera     opencamera else    showmycustompermissionview 

Couldn't find a current way to do this simple task.
Note: Should also work iOS7 even if it requires a different method

like image 573
Esqarrouth Avatar asked Dec 25 '14 09:12

Esqarrouth


2 Answers

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         }     }) } 
like image 197
Midhun MP Avatar answered Oct 12 '22 09:10

Midhun MP


Swift 3.0 Updated Solution

func callCamera(){     let myPickerController = UIImagePickerController()     myPickerController.delegate = self;     myPickerController.sourceType = UIImagePickerControllerSourceType.camera      self.present(myPickerController, animated: true, completion: nil)     NSLog("Camera"); } func checkCamera() {     let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)     switch authStatus {     case .authorized: callCamera() // Do your stuff here i.e. callCameraMethod()     case .denied: alertPromptToAllowCameraAccessViaSetting()     case .notDetermined: alertToEncourageCameraAccessInitially()     default: alertToEncourageCameraAccessInitially()     } }  func alertToEncourageCameraAccessInitially() {     let alert = UIAlertController(         title: "IMPORTANT",         message: "Camera access required for capturing photos!",         preferredStyle: UIAlertControllerStyle.alert     )     alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))     alert.addAction(UIAlertAction(title: "Allow Camera", style: .cancel, handler: { (alert) -> Void in         UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)     }))     present(alert, animated: true, completion: nil) }  func alertPromptToAllowCameraAccessViaSetting() {      let alert = UIAlertController(         title: "IMPORTANT",         message: "Camera access required for capturing photos!",         preferredStyle: UIAlertControllerStyle.alert     )     alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel) { alert in         if AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo).count > 0 {             AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { granted in                 DispatchQueue.main.async() {                     self.checkCamera() } }         }         }     )     present(alert, animated: true, completion: nil) } 
like image 37
Sourabh Sharma Avatar answered Oct 12 '22 10:10

Sourabh Sharma