Skip to content

v2.6.0

Latest

Choose a tag to compare

@github-actions github-actions released this 16 Jun 04:47
· 1 commit to main since this release

Full Changelog: https://github.com/jaywcjlove/PermissionFlow/commits/v2.6.0

Inject a status store at app startup

If you want to read permission state from any SwiftUI view, add the PermissionFlowStatusStore product:

.product(name: "PermissionFlow", package: "PermissionFlow"),
.product(name: "PermissionFlowStatusStore", package: "PermissionFlow")

Then create and inject the store at the app entry point:

import PermissionFlow
import PermissionFlowStatusStore
import SwiftUI

@main
struct MyApp: App {
    @StateObject private var permissionStatusStore = PermissionFlowStatusStore()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(permissionStatusStore)
        }
    }
}

Read it from any child view:

import PermissionFlow
import PermissionFlowStatusStore
import SwiftUI

struct PermissionBadge: View {
    @EnvironmentObject private var permissionStatusStore: PermissionFlowStatusStore

    var body: some View {
        Text(title(for: permissionStatusStore.state(for: .accessibility)))
            .onAppear {
                permissionStatusStore.refresh(.accessibility)
            }
    }

    private func title(for state: PermissionAuthorizationState) -> String {
        switch state {
        case .granted:
            "Granted"
        case .notGranted:
            "Not Granted"
        case .unknown:
            "Unknown"
        case .checking:
            "Checking"
        }
    }
}

PermissionFlowStatusStore tracks PermissionFlowPane.allCases by default and refreshes automatically when the app becomes active again. You can also track only selected panes:

@StateObject private var permissionStatusStore = PermissionFlowStatusStore(
    panes: [.accessibility, .fullDiskAccess, .screenRecording]
)

PermissionFlowStatusStore does not decide whether a pane is detectable by itself; it reads the providers currently registered in PermissionStatusRegistry. Current support is:

Pane Detectable by default Requires extra registration Not reliably detectable
.accessibility
.fullDiskAccess
.microphone
.bluetooth
.inputMonitoring
.mediaAppleMusic
.screenRecording
.appManagement
.developerTools

For panes that are not reliably detectable, state(for:) usually returns .unknown.

Note: PermissionFlowStatusStore is only the state container. Optional panes such as .inputMonitoring, .screenRecording, .bluetooth, and .mediaAppleMusic still need their status providers registered first. In other words, register() and PermissionFlowStatusStore are two separate steps:

import PermissionFlowInputMonitoringStatus
import PermissionFlowStatusStore
import SwiftUI

@main
struct MyApp: App {
    @StateObject private var permissionStatusStore: PermissionFlowStatusStore

    init() {
        PermissionFlowInputMonitoringStatus.register()
        _permissionStatusStore = StateObject(
            wrappedValue: PermissionFlowStatusStore()
        )
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(permissionStatusStore)
        }
    }
}

To enable all optional status providers at once, register PermissionFlowExtendedStatus:

import PermissionFlowExtendedStatus
import PermissionFlowStatusStore
import SwiftUI

@main
struct MyApp: App {
    @StateObject private var permissionStatusStore: PermissionFlowStatusStore

    init() {
        PermissionFlowExtendedStatus.register()
        _permissionStatusStore = StateObject(
            wrappedValue: PermissionFlowStatusStore()
        )
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(permissionStatusStore)
        }
    }
}