Esqarrouth Esqarrouth - 19 days ago 5
Swift Question

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

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

Answer

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

                }
            });
        }