Bird
Raised Fist0
iOS Swiftmobile~20 mins

Biometric authentication (Face ID, Touch ID) in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Biometric Authentication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
1:30remaining
What happens when biometric authentication succeeds?
In an iOS app using Face ID or Touch ID, what is the typical behavior after successful biometric authentication?
AThe app unlocks the protected content and navigates to the main screen.
BThe app closes immediately without any message.
CThe app shows an error message and asks to try again.
DThe app logs out the user automatically.
Attempts:
2 left
💡 Hint
Think about what the user expects after confirming their identity.
🧠 Conceptual
intermediate
1:00remaining
Which framework is used for biometric authentication in iOS?
Which Apple framework provides the APIs to implement Face ID and Touch ID authentication in iOS apps?
AUIKit
BCoreData
CAVFoundation
DLocalAuthentication
Attempts:
2 left
💡 Hint
It starts with 'Local' and deals with device security.
📝 Syntax
advanced
2:00remaining
What is the correct way to create a LAContext and check if biometric authentication is available?
Given the following Swift code snippet, which option correctly creates a LocalAuthentication context and checks if biometric authentication can be evaluated?
iOS Swift
import LocalAuthentication

let context = LAContext()
var error: NSError?

// Check if biometric authentication is available
let canEvaluate = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
Alet canEvaluate = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
Blet canEvaluate = context.canEvaluatePolicy(.deviceOwnerAuthentication, error: error)
Clet canEvaluate = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: error)
Dlet canEvaluate = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics)
Attempts:
2 left
💡 Hint
Check the method signature and parameter types carefully.
❓ lifecycle
advanced
1:30remaining
What happens if biometric authentication is interrupted by app going to background?
If the user starts biometric authentication but the app goes to the background (e.g., user presses home button), what is the expected behavior?
AThe authentication succeeds immediately without user input.
BThe app crashes due to lost context.
CThe authentication is cancelled and the app must restart it when active again.
DThe authentication continues in the background automatically.
Attempts:
2 left
💡 Hint
Think about security and user interaction requirements.
🔧 Debug
expert
2:30remaining
Why does this biometric authentication code always fail with error code -7?
Consider this Swift code snippet for biometric authentication: import LocalAuthentication let context = LAContext() context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Access needed") { success, error in if success { print("Authenticated") } else { print(error?.localizedDescription ?? "Unknown error") } } The error code returned is always -7 (LAError.biometryNotAvailable). What is the most likely cause?
AThe LAContext instance must be created inside the closure.
BThe device does not have biometric hardware or it is disabled in settings.
CThe localizedReason string is missing and causes failure.
DThe evaluatePolicy method requires a completion handler on the main thread.
Attempts:
2 left
💡 Hint
Error code -7 means biometry is not available on the device.

Practice

(1/5)
1. What is the main purpose of using biometric authentication like Face ID or Touch ID in an iOS app?
easy
A. To change the app's theme automatically
B. To improve app loading speed
C. To securely verify the user's identity without typing a password
D. To send notifications to the user

Solution

  1. Step 1: Understand biometric authentication purpose

    Biometric authentication is designed to verify the user's identity securely using Face ID or Touch ID.
  2. Step 2: Compare options with purpose

    Only To securely verify the user's identity without typing a password describes secure identity verification without passwords, which is the main goal.
  3. Final Answer:

    To securely verify the user's identity without typing a password -> Option C
  4. Quick Check:

    Biometric authentication = Secure identity check [OK]
Hint: Biometrics replace passwords for secure login [OK]
Common Mistakes:
  • Thinking biometrics speed up app loading
  • Confusing biometric use with UI changes
  • Assuming biometrics send notifications
2. Which Swift code snippet correctly checks if biometric authentication is available before using it?
easy
A. let context = LAContext() if context.evaluatePolicy(.deviceOwnerAuthentication, error: nil) { // proceed }
B. let context = LAContext() var error: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) { // proceed }
C. let context = LAContext() if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) { // proceed }
D. let context = LAContext() if context.canAuthenticate() { // proceed }

Solution

  1. Step 1: Identify correct method to check biometric availability

    The method canEvaluatePolicy with .deviceOwnerAuthenticationWithBiometrics checks if biometrics are available.
  2. Step 2: Verify code syntax and usage

    let context = LAContext() var error: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) { // proceed } uses canEvaluatePolicy with correct policy and proper &error parameter, matching Apple's guidelines.
  3. Final Answer:

    let context = LAContext()\nvar error: NSError?\nif context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {\n // proceed\n} -> Option B
  4. Quick Check:

    Check biometrics availability = canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics) [OK]
Hint: Use canEvaluatePolicy with biometric policy to check availability [OK]
Common Mistakes:
  • Using evaluatePolicy instead of canEvaluatePolicy for availability
  • Using wrong policy like .deviceOwnerAuthentication
  • Calling non-existent methods like canAuthenticate
3. Given this Swift code snippet, what will be printed if biometric authentication succeeds?
let context = LAContext()
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Access needed") { success, error in
  if success {
    print("Authentication succeeded")
  } else {
    print("Authentication failed")
  }
}
medium
A. Authentication succeeded
B. No output
C. Authentication failed
D. Syntax error

Solution

  1. Step 1: Understand evaluatePolicy callback behavior

    The closure receives a success boolean indicating if authentication passed.
  2. Step 2: Check the if condition for success

    If success is true, it prints "Authentication succeeded"; otherwise, it prints failure.
  3. Final Answer:

    Authentication succeeded -> Option A
  4. Quick Check:

    Success true prints "Authentication succeeded" [OK]
Hint: Success true means authentication passed and prints success message [OK]
Common Mistakes:
  • Confusing success and failure branches
  • Assuming no output from async closure
  • Thinking code has syntax errors
4. Identify the error in this Swift code for biometric authentication:
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: error) {
  context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Login") { success, error in
    if success {
      print("Access granted")
    }
  }
}
medium
A. The error parameter should be passed as &error to be mutable
B. evaluatePolicy should be called before canEvaluatePolicy
C. The localizedReason string is missing
D. LAContext must be declared as a class, not a let constant

Solution

  1. Step 1: Check error parameter usage in canEvaluatePolicy

    The error parameter must be passed as an inout pointer using &error to capture errors.
  2. Step 2: Verify other code parts

    evaluatePolicy is correctly called after canEvaluatePolicy, localizedReason is provided, and LAContext can be a let constant.
  3. Final Answer:

    The error parameter should be passed as &error to be mutable -> Option A
  4. Quick Check:

    Error param must be &error for canEvaluatePolicy [OK]
Hint: Pass error as &error to canEvaluatePolicy for error capture [OK]
Common Mistakes:
  • Passing error without & causing no error info
  • Calling evaluatePolicy before checking availability
  • Missing localizedReason string
  • Misunderstanding let vs var for LAContext
5. You want to allow users to authenticate using Face ID or Touch ID, but fallback to device passcode if biometrics fail or are unavailable. Which Swift code snippet correctly implements this behavior?
hard
A. let context = LAContext() var error: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) { context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Authenticate") { success, error in if success { print("Biometric success") } else { print("Authentication failed") } } } else { print("Biometrics not available") }
B. let context = LAContext() context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Authenticate") { success, error in if success { print("Authentication success") } else { print("Authentication failed") } }
C. let context = LAContext() if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) { context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Authenticate") { success, error in if success { print("Biometric success") } else { context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Fallback") { success, error in if success { print("Passcode success") } else { print("Authentication failed") } } } } }
D. let context = LAContext() var error: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) { context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Authenticate") { success, error in if success { print("Authentication success") } else { print("Authentication failed") } } }

Solution

  1. Step 1: Understand fallback requirement

    The app should try biometrics first, then fallback to device passcode if biometrics fail or are unavailable.
  2. Step 2: Analyze code options for fallback logic

    let context = LAContext() var error: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) { context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Authenticate") { success, error in if success { print("Authentication success") } else { print("Authentication failed") } } } uses .deviceOwnerAuthentication policy which automatically prefers biometrics (Face ID/Touch ID) and falls back to passcode if unavailable or fails. It also properly checks availability first per Apple's guidelines.
  3. Final Answer:

    let context = LAContext()\nvar error: NSError?\nif context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) {\n context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: \"Authenticate\") { success, error in\n if success {\n print(\"Authentication success\")\n } else {\n print(\"Authentication failed\")\n }\n }\n} -> Option D
  4. Quick Check:

    .deviceOwnerAuthentication policy = biometric fallback to passcode [OK]
Hint: Use .deviceOwnerAuthentication policy with canEvaluatePolicy check for fallback [OK]
Common Mistakes:
  • Using biometrics-only policy without fallback (Options A, B)
  • Direct evaluation without availability check
  • Syntax error with error: nil and incomplete fallback (Option D)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard