With the increasing reliance on biometric authentication for secure and seamless access, developers must understand how to integrate these features effectively. By breaking down the process, this post empowers developers to build more secure and user-friendly applications, aligning with Apple's emphasis on privacy and cutting-edge technology.

In this micro-post, you'll see just how easy it is to implement biometric authentication on iOS.

Authentication App

Before start codign we need to fill in a message on ‘FaceIDUssageAuthentication:

This is the view:

struct ContentView: View {
    @State private var isAuthenticated = false
    @State private var errorMessage = ""

    var body: some View {
        VStack {
            if isAuthenticated {
                Text("Authentication successful!")
                    .font(.title)
                    .foregroundColor(.green)
            } else {
                Text(errorMessage)
                    .font(.title)
                    .foregroundColor(.red)
            }

            Button(action: {
                authenticate()
            }) {
                Text("Authenticate with Touch ID / Face ID")
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }
        }
        .padding()
    }

This code creates a simple SwiftUI view for handling biometric authentication. It displays a success or error message based on the authentication status and provides a button to trigger the authentication process. 

And this is the authentication code:

    func authenticate() {
        let context = LAContext()
        var error: NSError?

        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
            let reason = "Authenticate for having access to application"

            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
                DispatchQueue.main.async {
                    if success {
                        isAuthenticated = true
                        errorMessage = ""
                    } else {
                        isAuthenticated = false
                        errorMessage = "Failed authentication"
                    }
                }
            }
        } else {
            isAuthenticated = false
            errorMessage = "Touch ID / Face ID no está disponible"
        }
    }

The authenticate() function uses the Local Authentication framework to enable biometric authentication (Touch ID or Face ID) for accessing an application. It first checks if the device supports biometric authentication using canEvaluatePolicy. If supported, it prompts the user to authenticate with a reason message, and upon success, sets isAuthenticated to true and clears any error messages; if authentication fails, it sets isAuthenticated to false and updates errorMessage to indicate the failure. If biometric authentication is unavailable or not configured, it sets isAuthenticated to false and updates errorMessage to reflect that Touch ID/Face ID is not available. The function ensures UI updates are performed on the main thread, making it suitable for integration into apps requiring secure user access.

Finally deploy in a real device:

Conclusions

As you can see in the code above, it is easy to integrate the same biometric authentication mechanism used to unlock the iPhone into your apps.

You can find source code used for writing this post in following repository

References

Copyright © 2024-2025 JaviOS. All rights reserved