Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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