Here is a simple example:
struct ContentView: View {
@State private var isUnlocked = false
var body: some View {
VStack {
if isUnlocked {
Text("Unlocked")
} else {
Text("Locked")
}
}
.padding()
.onAppear(perform: authenticate)
}
}
import LocalAuthentication
func authenticate() {
let context = LAContext()
var error: NSError?
// check whether biometric authentication is possible
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
// it's possible, so go ahead and use it
let reason = "We need to unlock your data."
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
// authentication has now completed
if success {
// authenticated successfully
} else {
// there was a problem
if let error = authenticationError as? NSError {
print("Authentication failed: \(error), \(error.userInfo)")
} else {
print("Authentication failed: \(String(describing: authenticationError))")
}
}
}
} else {
// no biometrics
}
}
Here is the error it spits back:
Authentication failed: Error Domain=com.apple.LocalAuthentication Code=-1000 "UI service connection invalidated." UserInfo={NSDebugDescription=UI service connection invalidated., NSLocalizedDescription=Authentication failure.}, ["NSDebugDescription": UI service connection invalidated., "NSLocalizedDescription": Authentication failure.]
I get this error pop up from Xcode:
"CoreAuthUI quit unexpectedly."
This may help from the Problem Report:
-------------------------------------
Translated Report (Full Report Below)
-------------------------------------
Incident Identifier: 44ECD006-5C1C-4E2B-A580-003F46E298E5
CrashReporter Key: 74F42D79-158E-2606-618B-4DC2CCB64CD7
Hardware Model: Mac15,9
Process: CoreAuthUI [13984]
Path: /Volumes/VOLUME/*/CoreAuthUI.app/CoreAuthUI
Identifier: com.apple.CoreAuthUI
Version: 1.0 (1656.40.15)
Code Type: ARM-64 (Native)
Role: Foreground
Parent Process: launchd_sim [13492]
Coalition: com.apple.CoreSimulator.SimDevice.BCC8C055-BE32-4735-BA12-890CFD1512FC [3618]
Responsible Process: SimulatorTrampoline [1694]
Date/Time: 2024-09-02 21:20:46.9564 -0700
Launch Time: 2024-09-02 21:20:46.4760 -0700
OS Version: macOS 14.6.1 (23G93)
Release Type: User
Report Version: 104
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: SIGNAL 6 Abort trap: 6
Terminating Process: CoreAuthUI [13984]
I have the plist entry:
Privacy - Face ID Usage Description = "Allow the app to use FaceID"