Skip to content

Instantly share code, notes, and snippets.

@saroar
Created February 1, 2024 19:00
Show Gist options
  • Save saroar/1e3187d5be74276bb6bb33ccce7dd966 to your computer and use it in GitHub Desktop.
Save saroar/1e3187d5be74276bb6bb33ccce7dd966 to your computer and use it in GitHub Desktop.
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