Complete the code to create a LocalAuthentication context.
let context = LAContext() let reason = "Authenticate to access your account" context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, error in if success { print("Authentication [1]") } }
The success boolean is true when authentication succeeds, so the message should say "succeeded".
Complete the code to check if biometric authentication is available.
let context = LAContext() var error: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &[1]) { print("Biometrics available") } else { print("Biometrics not available") }
The canEvaluatePolicy method requires an NSError pointer variable named error to store any error details.
Fix the error in the code to handle authentication failure.
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, [1] in
if !success {
print("Authentication failed: \([1]?.localizedDescription ?? \"Unknown error\")")
}
}The closure parameter for the error is conventionally named error, matching the API signature.
Fill both blanks to create a dictionary comprehension filtering biometrics types.
let biometrics = ["FaceID": context.biometryType == [1], "TouchID": context.biometryType == [2]] print(biometrics)
The biometryType property is compared to LABiometryType.faceID and LABiometryType.touchID to check which biometric is available.
Fill all three blanks to create a biometric authentication function with completion handler.
func authenticateUser(completion: @escaping (Bool) -> Void) {
let context = LAContext()
let reason = "Please authenticate"
context.evaluatePolicy([1], localizedReason: reason) { success, error in
DispatchQueue.main.async {
if success {
completion([2])
} else {
completion([3])
}
}
}
}Using .deviceOwnerAuthentication allows biometrics or passcode fallback. The completion handler receives true on success and false on failure.