Created
February 1, 2024 19:00
-
-
Save saroar/1e3187d5be74276bb6bb33ccce7dd966 to your computer and use it in GitHub Desktop.
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
import AVFoundation | |
import Contacts | |
import CoreLocation | |
import Foundation | |
import UserNotifications | |
@DependencyClient | |
public struct DevicePermissions { | |
enum PermissionError: Error { | |
case cameraAccessDenied | |
case microphoneAccessDenied | |
case contactAccessDenied | |
case locationAccessDenied | |
case notificationAccessDenied | |
} | |
public var requestCameraPermissions: @Sendable () async throws -> Bool | |
public var requestMicrophonePermissions: @Sendable () async throws -> Void | |
public var requestContactPermissions: @Sendable () async throws -> Void | |
public var requestLocationPermissions: @Sendable () async throws -> Void | |
public var requestNotificationPermissions: @Sendable () async throws -> Void | |
} | |
extension DevicePermissions { | |
public static var live: DevicePermissions = .init( | |
requestCameraPermissions: { | |
let status = AVCaptureDevice.authorizationStatus(for: .video) | |
// Determine if the user previously authorized camera access. | |
var isAuthorized = status == .authorized | |
// If the system hasn't determined the user's authorization status, | |
// explicitly prompt them for approval. | |
if status == .notDetermined { | |
isAuthorized = await AVCaptureDevice.requestAccess(for: .video) | |
} | |
return isAuthorized | |
}, | |
requestMicrophonePermissions: { | |
}, | |
requestContactPermissions: { | |
}, | |
requestLocationPermissions: { | |
}, | |
requestNotificationPermissions: { | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment