0
0
iOS Swiftmobile~10 mins

Biometric authentication (Face ID, Touch ID) in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a LocalAuthentication context.

iOS Swift
let context = LAContext()
let reason = "Authenticate to access your account"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, error in
    if success {
        print("Authentication [1]")
    }
}
Drag options to blanks, or click blank then click option'
Apending
Bcancelled
Cfailed
Dsucceeded
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'failed' instead of 'succeeded'
Confusing the success boolean meaning
2fill in blank
medium

Complete the code to check if biometric authentication is available.

iOS Swift
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &[1]) {
    print("Biometrics available")
} else {
    print("Biometrics not available")
}
Drag options to blanks, or click blank then click option'
Aerror
Berr
Cfailure
DauthError
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not declared
Forgetting the & before the variable
3fill in blank
hard

Fix the error in the code to handle authentication failure.

iOS Swift
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, [1] in
    if !success {
        print("Authentication failed: \([1]?.localizedDescription ?? \"Unknown error\")")
    }
}
Drag options to blanks, or click blank then click option'
Aerror
Berr
Cfailure
DauthError
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong parameter name causing compile errors
Not handling optional error safely
4fill in blank
hard

Fill both blanks to create a dictionary comprehension filtering biometrics types.

iOS Swift
let biometrics = ["FaceID": context.biometryType == [1], "TouchID": context.biometryType == [2]]
print(biometrics)
Drag options to blanks, or click blank then click option'
ALABiometryType.faceID
BLABiometryType.touchID
CLABiometryType.none
DLABiometryType.fingerprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong enum names
Confusing FaceID with TouchID constants
5fill in blank
hard

Fill all three blanks to create a biometric authentication function with completion handler.

iOS Swift
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])
            }
        }
    }
}
Drag options to blanks, or click blank then click option'
A.deviceOwnerAuthenticationWithBiometrics
Btrue
Cfalse
D.deviceOwnerAuthentication
Attempts:
3 left
💡 Hint
Common Mistakes
Using only biometrics policy without fallback
Not calling completion on main thread
Swapping true and false in completion