@@ -19,23 +19,27 @@ import AVFoundation
19
19
/// Handles checking and asking for camera access permission.
20
20
class CameraAccessHandler {
21
21
22
+ /// Returns `true` if the user authorized access to the device camera.
23
+ static var hasGrantedAccess : Bool {
24
+ AVCaptureDevice . authorizationStatus ( for: . video) == . authorized
25
+ }
26
+
22
27
/// Checks the permissions status of the camera, and requests access if needed.
23
28
///
24
29
/// - Parameter requestCompletion: Called with the status of the permission (true if granted),
25
- /// when access had to be requested.
26
- /// - Returns: Whether or not permission has been granted.
27
- static func checkForPermission( requestCompletion: ( ( Bool ) -> Void ) ? = nil ) -> Bool {
30
+ static func checkForPermission( requestCompletion: ( ( Bool ) -> Void ) ? = nil ) {
28
31
let authStatus = AVCaptureDevice . authorizationStatus ( for: . video)
29
32
switch authStatus {
30
- case . denied, . restricted: return false
31
- case . authorized: return true
33
+ case . denied, . restricted: requestCompletion ? ( false )
34
+ case . authorized: requestCompletion ? ( true )
32
35
case . notDetermined:
33
36
// Prompt user for the permission to use the camera.
34
37
AVCaptureDevice . requestAccess ( for: . video) { granted in
35
- requestCompletion ? ( granted)
38
+ DispatchQueue . main. async {
39
+ requestCompletion ? ( granted)
40
+ }
36
41
}
37
- return false
38
- @unknown default : return false
42
+ @unknown default : requestCompletion ? ( false )
39
43
}
40
44
}
41
45
0 commit comments