Created
January 2, 2017 23:07
-
-
Save b-r-o/8d3f298d5bd224a4407088453706f1ac to your computer and use it in GitHub Desktop.
Clean permission request mechanism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
init() { | |
checkPermission() | |
sessionQueue.async { [unowned self] in | |
self.configureSession() | |
self.captureSession.startRunning() | |
} | |
} | |
// MARK: AVSession configuration | |
private func checkPermission() { | |
switch AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) { | |
case .authorized: | |
permissionGranted = true | |
case .notDetermined: | |
requestPermission() | |
default: | |
permissionGranted = false | |
} | |
} | |
private func requestPermission() { | |
sessionQueue.suspend() | |
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { [unowned self] granted in | |
self.permissionGranted = granted | |
self.sessionQueue.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment