Skip to content

Instantly share code, notes, and snippets.

@rorodriguez116
Created October 22, 2020 16:20
Show Gist options
  • Save rorodriguez116/513619c91a7b228c0c8e01aa1d9453d6 to your computer and use it in GitHub Desktop.
Save rorodriguez116/513619c91a7b228c0c8e01aa1d9453d6 to your computer and use it in GitHub Desktop.
Function to check camera access permissions.
// MARK: Checks for user's permisions
public func checkForPermissions() {
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized:
// The user has previously granted access to the camera.
break
case .notDetermined:
/*
The user has not yet been presented with the option to grant
video access. Suspend the session queue to delay session
setup until the access request has completed.
*/
sessionQueue.suspend()
AVCaptureDevice.requestAccess(for: .video, completionHandler: { granted in
if !granted {
self.setupResult = .notAuthorized
}
self.sessionQueue.resume()
})
default:
// The user has previously denied access.
// Store this result, create an alert error and tell the UI to show it.
setupResult = .notAuthorized
DispatchQueue.main.async {
self.alertError = AlertError(title: "Camera Access", message: "SwiftCamera doesn't have access to use your camera, please update your privacy settings.", primaryButtonTitle: "Settings", secondaryButtonTitle: nil, primaryAction: {
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!,
options: [:], completionHandler: nil)
}, secondaryAction: nil)
self.shouldShowAlertView = true
self.isCameraUnavailable = true
self.isCameraButtonDisabled = true
}
}
}
@alexward17
Copy link

alexward17 commented Mar 6, 2023

Hello! Thanks for this helpful function. Could I ask what type of queue is self.sesionQueue and how it's used in this context?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment