How do I handle biometric authentication with Face ID/Touch ID?

Handling biometric authentication using Face ID or Touch ID in your Swift applications offers a seamless user experience while enhancing security. Below is a simple implementation example of how to integrate biometric authentication in your app.

Example of Biometric Authentication

import LocalAuthentication func authenticateUser() { let context = LAContext() var error: NSError? if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) { let reason = "Authentication is required to access this feature." context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in DispatchQueue.main.async { if success { // User authenticated successfully print("Authentication succeeded!") } else { // User did not authenticate print("Authentication failed: \(authenticationError?.localizedDescription ?? "")") } } } } else { // Biometrics not available or not configured print("Biometrics not available.") } }

biometric authentication face id touch id swift biometric local authentication ios security