Files

25 lines
951 B
Swift

import Foundation
import LocalAuthentication
/// Face ID / Touch ID gating for the app lock and for sensitive actions.
enum Biometrics {
static var isAvailable: Bool {
LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
}
static var label: String {
let context = LAContext()
_ = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
return context.biometryType == .faceID ? "Face ID" : "Touch ID"
}
/// Biometrics with the system passcode fallback (the alert offers "Use
/// Passcode" after a failed face scan).
static func authenticate(reason: String) async -> Bool {
guard isAvailable else { return false }
let context = LAContext()
context.localizedCancelTitle = "Cancel"
return (try? await context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,
localizedReason: reason)) ?? false
}
}