LAContext: canEvaluatePolicyError() method
The canEvaluatePolicyError() method answers why authentication with a given policy is not possible. It returns synchronously and shows no UI.
Not being able to authenticate is a normal answer, so the reason is handed back as the return value rather than thrown.
Syntax
canEvaluatePolicyError(policy)Parameters
policy
The policy, 'deviceOwnerAuthenticationWithBiometrics' or 'deviceOwnerAuthentication'; see Policies.
Return value
nullwhencanEvaluatePolicy()returnstrue.- When it returns
false, anErrorobject, a new one on each call. It is the same kind thatevaluatePolicy()rejects with when authentication fails, carryingname,message,code, anddomain; see Reasons authentication failed.messageis the system's explanation, such asBiometry is not enrolled.;domainis always'com.apple.LocalAuthentication'.
In this case name is one of the following:
name | Meaning |
|---|---|
'BiometryNotAvailableError' | The device does not support biometrics, or the user has not allowed this app to use Face ID |
'BiometryNotEnrolledError' | The device supports it, but the user has not enrolled a face or fingerprint |
'BiometryLockoutError' | Too many failures locked out biometrics; the passcode must be entered once first |
'PasscodeNotSetError' | The device has no passcode set |
'InvalidContextError' | invalidate() has been called on this object |
BiometryNotEnrolledError and InvalidContextError are verified in the simulator. The other three are listed from the system headers and are not verified in the simulator.
Exceptions
TypeError
The policy is unrecognized, missing, or not a string:
canEvaluatePolicyError has no value "biometrics"; expected one of: deviceOwnerAuthenticationWithBiometrics / deviceOwnerAuthenticationExamples
const context = new LAContext();
const error = context.canEvaluatePolicyError('deviceOwnerAuthenticationWithBiometrics');
console.log(error === null ? 'Can authenticate' : error.name);Result: Prints Can authenticate when authentication is possible, and otherwise the name of the reason. On a simulator with no face enrolled, prints BiometryNotEnrolledError.
Corresponding iOS API
The error out-parameter of -[LAContext canEvaluatePolicy:error:]. The return-value half is returned by canEvaluatePolicy().