Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hashaam
Last active August 16, 2020 16:54
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/5535e15e778885ead26f57a15e973045 to your computer and use it in GitHub Desktop.
Save hashaam/5535e15e778885ead26f57a15e973045 to your computer and use it in GitHub Desktop.
// Youtube: https://www.youtube.com/watch?v=7AlZxClmhPw
// Source: https://hashaam.com/2020/07/30/creating-camera-application-with-avfoundation/
import Foundation
import AVFoundation
enum CameraAuthorizationStatus {
case notRequested
case granted
case unauthorized
}
typealias RequestCameraAuthorizationCompletionHandler = (CameraAuthorizationStatus) -> Void
class RequestCameraAuthorizationController {
static func requestCameraAuthorization(completionHandler: @escaping RequestCameraAuthorizationCompletionHandler) {
AVCaptureDevice.requestAccess(for: .video, completionHandler: { granted in
DispatchQueue.main.async {
guard granted else {
completionHandler(.unauthorized)
return
}
completionHandler(.granted)
}
})
}
static func getCameraAuthorizationStatus() -> CameraAuthorizationStatus {
let status = AVCaptureDevice.authorizationStatus(for: .video)
switch status {
case .authorized: return .granted
case .notDetermined: return .notRequested
default: return .unauthorized
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment