Biometric authentication lets users unlock apps using their face or fingerprint. It makes logging in faster and more secure without typing passwords.
To let users quickly unlock a banking app using Face ID or Touch ID.
To protect sensitive information in a health app with fingerprint authentication.
To allow easy login to a notes app without entering a password every time.
To confirm a purchase or payment securely using biometric data.
To add an extra layer of security for user profiles in social media apps.
Syntax
iOS Swift
import LocalAuthentication
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Access requires authentication") { success, error inif success {
// Authentication successful
} else {
// Authentication failed
}
}
} else {
// Biometrics not available ornot enrolled
}
Use LAContext to access biometric features.
Always check if biometrics are available before trying to authenticate.
Examples
This checks if the device supports Face ID or Touch ID.
iOS Swift
let context = LAContext()
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
print("Biometrics available")
} else {
print("Biometrics not available")
}
This asks the user to authenticate using biometrics and handles success or failure.
iOS Swift
let context = LAContext()
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Log in with Face ID or Touch ID") { success, error inif success {
print("User authenticated")
} else {
print("Authentication failed")
}
}
Sample App
This SwiftUI app shows a button to start biometric authentication. It updates the text message based on success or failure.
iOS Swift
import SwiftUI
import LocalAuthentication
struct ContentView: View {
@State private var message = "Please authenticate"
var body: some View {
VStack(spacing: 20) {
Text(message)
.padding()
Button("Authenticate") {
authenticateUser()
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
}
.padding()
}
func authenticateUser() {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Unlock with Face ID or Touch ID") { success, authenticationError in
DispatchQueue.main.async {
if success {
message = "Authentication successful!"
} else {
message = "Authentication failed. Try again."
}
}
}
} else {
DispatchQueue.main.async {
message = "Biometric authentication not available."
}
}
}
}
OutputSuccess
Important Notes
Test biometric features on a real device; simulators do not support Face ID or Touch ID.
Always provide a fallback option if biometrics are not available or fail.
Use clear messages to guide users during authentication.
Summary
Biometric authentication uses Face ID or Touch ID to secure apps easily.
Check availability before asking for authentication.
Handle success and failure to update the app UI accordingly.
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
Biometric authentication is designed to verify the user's identity securely using Face ID or Touch ID.
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.
Final Answer:
To securely verify the user's identity without typing a password -> Option C
The closure receives a success boolean indicating if authentication passed.
Step 2: Check the if condition for success
If success is true, it prints "Authentication succeeded"; otherwise, it prints failure.
Final Answer:
Authentication succeeded -> Option A
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
Step 1: Check error parameter usage in canEvaluatePolicy
The error parameter must be passed as an inout pointer using &error to capture errors.
Step 2: Verify other code parts
evaluatePolicy is correctly called after canEvaluatePolicy, localizedReason is provided, and LAContext can be a let constant.
Final Answer:
The error parameter should be passed as &error to be mutable -> Option A
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
Step 1: Understand fallback requirement
The app should try biometrics first, then fallback to device passcode if biometrics fail or are unavailable.
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.