Apple does not provide a universal API for managing permission statuses. Their frameworks have own status models, request mechaninsms and status handlers.
Also, Apple frameworks' permission status management is not testable.
There is a universal status model.
enum PermissionStatus {
/// A status that indicates the user has explicitly
/// granted an app permission to a capability.
case granted
/// A status that indicates the user has explicitly
/// denied an app permission to a capability.
case denied
/// A status that indicates the user hasn’t
/// yet granted or denied authorization.
case undetermined
/// A status that indicates a capability is not supported.
case unsupported
}
There are several microlibraries. Each one has a manager for a certain status kind.
Name | Apple framework | Microlibrary to import |
---|---|---|
Camera | AVFoundation | ComposableCameraPermission |
Health | HealthKit | ComposableHealthPermission |
Local auth | LocalAuthentication | ComposableLocalAuthenticationPermission |
Location | CoreLocation | ComposableLocationPermission |
Motion | CoreMotion | ComposableMotionPermission |
Photo library | Photos | ComposablePhotoLibraryPermission |
Tracking | AppTrackingTransparency | ComposableTrackingPermission |
Each permission manager is a dependency client (see swift-dependencies). This approach provides testability, and allows to easily integrate the library into a TCA project.
There is an example how to use a location permission client
import ComposableLocationPermission
@Dependency(\.locationPermissionClient)
var locationPermission
let whenInUsePermissionStatus = await locationPermission.requestWhenInUse()
if whenInUsePermissionStatus == .granted {
...
}
You can add the library to an Xcode project by adding it as a package dependency.
If you want to use the library in a SwiftPM project, it's as simple as adding it to a dependencies
clause in your Package.swift
:
dependencies: [
.package(url: "https://github.com/whutao/swift-composable-permissions", from: "1.0.0")
]
and then adding needed microlibraries to a target dependencies
.target(name: ..., dependencies: [
.product(
name: "ComposableLocationPermission",
package: "swift-composable-permissions"
)
])
All modules are released under the MIT license. See LICENSE for details.