Cotton code
30 lines · 1.3 KB · SwiftRaw
// THE PHONE'S OWN LOCK, asked by any app that keeps something out of sight behind it: Sites' hidden sites, Cinemas'// Hidden. One helper, so every app asks it the same way and says the same thing when the phone has none.#if os(iOS)import LocalAuthenticationimport UIKitenum DeviceLock {    /// Whether this phone has a screen lock (Face ID, Touch ID or a passcode) to ask.    static var isSet: Bool {        var error: NSError?        return LAContext().canEvaluatePolicy(.deviceOwnerAuthentication, error: &error)    }    /// The phone's own lock, asked with `reason`: true once it passes. False when it fails, is cancelled, or the phone    /// has no lock (ask `isSet` first to tell that apart).    static func ask(_ reason: String) async -> Bool {        let context = LAContext()        var error: NSError?        guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) else { return false }        return (try? await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason)) == true    }    /// The phone's Settings, where a screen lock is set (iOS opens no deeper page than the app's own for anyone).    @MainActor static func openSettings() {        guard let url = URL(string: UIApplication.openSettingsURLString) else { return }        UIApplication.shared.open(url)    }}#endif