I'm trying to change a state bool var with faceID.
My Test Code.
import SwiftUI
import LocalAuthentication
struct PasswordEditTest: View {
@State private var showPassword: Bool = false
var body: some View {
HStack {
Section(header: Text("Password:").foregroundColor(Color.theme.red)
) {
if showPassword {
Text("UNLOCKED")
}
else {
Text("LOCKED")
}
}
Spacer()
Image(systemName: showPassword ? "lock.open" : "lock")
.resizable()
.scaledToFill()
.frame(width: showPassword ? 30 : 30, height: showPassword ? 39 : 39)
.padding()
.onTapGesture {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let reason = "We need to unlock your password"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
if success {
print("SUCCESS")
showPassword=true
} else {
print("FAIL")
showPassword=false
}
}
} else {
//NO FACEID/Biometrics
}
}
}
}
}
If I change the showPassword to TRUE on the tap gesture, once the faceid success fires it will change the var back to false, So strange. It was working fine on iOS16
.onTapGesture {
showPassword=true //ADD THIS
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let reason = "We need to unlock your password"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
if success {
// the showPassword will change back to false.
print("SUCCESS")
showPassword=true. // This will not change it to true Crazy!
} else {
print("FAIL")
showPassword=false
}
}
} else {
//NO FACEID/Biometrics
}
evaluatePolicystates that the closure is executed in an unspecified threading context so you should dispatch your state update onto the main queue.