iOS. How to know if faceid prompt is presented?

350 Views Asked by At

Is there any way (in swift) to know if the system faceId prompt is being presented? I can't see any event, notification or delegate method. I should avoid the presentation of a view triggered by an asynchronous event in case the app is trying to authenticate the user.

1

There are 1 best solutions below

0
On

There is no specific event, notification, or delegate method in Swift that allows you to determine if the system Face ID prompt is being presented. However, you can use the LAContext class to check the availability and state of Face ID on the device, and then use that information to determine if the prompt is likely to be displayed.

You can use the canEvaluatePolicy method of LAContext to check if the device supports Face ID and if the user has configured it.

let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
    //FaceID is available
}

Then you can use the evaluatePolicy method to check if the user already authenticated recently or not.

context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Log in with Face ID") { success, error in
if success {
    // Face ID authentication was successful
} else {
    // Face ID authentication failed
}

}

It's important to notice that you should avoid presenting a view triggered by an asynchronous event in case the app is trying to authenticate the user.