0
0
iOS Swiftmobile~5 mins

Biometric authentication (Face ID, Touch ID) in iOS Swift

Choose your learning style9 modes available
Introduction

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 in
        if success {
            // Authentication successful
        } else {
            // Authentication failed
        }
    }
} else {
    // Biometrics not available or not 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 in
    if 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.