Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashaam/6483480ad1557927c60539064584578a to your computer and use it in GitHub Desktop.
Save hashaam/6483480ad1557927c60539064584578a to your computer and use it in GitHub Desktop.
Request Camera Authorization - Snippet 2
import UIKit
import AVFoundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let authorizationStatus = getCameraAuthorization()
switch authorizationStatus {
case .notDetermined:
print("Camera permission has not yet granted. We can request camera permission from the user")
// 2: Call the Request Camera Authorization method
requestCameraAuthorization { granted in
// 3: Print the status
let grantedStatus = granted ? "granted" : "not granted"
print("Camera permission was \(grantedStatus)")
}
case .restricted:
print("User is not allowed to access media capture device.")
case .denied:
print("User has denied accessing camera. We need to request user to allow the camera permission from settings.")
case .authorized:
print("Camera permission was granted")
@unknown default:
print("Camera authorization status is unknown.")
}
}
func getCameraAuthorization() -> AVAuthorizationStatus {
let authorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
return authorizationStatus
}
// 1: Create method to Request Camera Authorization
func requestCameraAuthorization(completionHandler: @escaping (Bool) -> Void) {
AVCaptureDevice.requestAccess(for: .video) { granted in
completionHandler(granted)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment