DEV Community

Cover image for Implementing Biometric Authentication in Android and iOS Apps ๐Ÿ”‘โœจ
Info general Hazedawn
Info general Hazedawn

Posted on

Implementing Biometric Authentication in Android and iOS Apps ๐Ÿ”‘โœจ

Biometric authentication is becoming a must-have feature in mobile apps. It provides users with a secure, seamless way to authenticate using their fingerprints, face, or other unique identifiers. This guide will walk you through implementing biometric authentication in Android and iOS apps. Let's dive in! ๐ŸŒŠ


Why Use Biometric Authentication? โ“

  1. Enhanced Security โ€” Biometric data is unique to each user and difficult to replicate.
  2. User Convenience โ€” No need to remember complex passwords.
  3. Improved UX โ€” Instant access with just a touch or a glance.

Prerequisites โš™๏ธ

For Android:

  • Android Studio installed.
  • A physical device or emulator with biometric support.
  • Minimum SDK version: 23 (Android 6.0).

For iOS:

  • Xcode installed.
  • iOS device or simulator with biometric support.
  • Deployment target: iOS 11.0 or later.

Android Implementation ๐Ÿง

Android provides the BiometricPrompt API to handle biometric authentication. Hereโ€™s how you can implement it:

Step 1: Add Dependencies

implementation 'androidx.biometric:biometric:1.2.0-alpha05'
Enter fullscreen mode Exit fullscreen mode

Step 2: Request Biometric Permission

Add the following permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.USE_BIOMETRIC" />
Enter fullscreen mode Exit fullscreen mode

Step 3: Code Implementation

Hereโ€™s a sample implementation:

val biometricPrompt = BiometricPrompt(this, ContextCompat.getMainExecutor(this),
    object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            Toast.makeText(this@MainActivity, "Authentication successful! ๐Ÿ”‘", Toast.LENGTH_SHORT).show()
        }

        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            Toast.makeText(this@MainActivity, "Authentication error: $errString", Toast.LENGTH_SHORT).show()
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            Toast.makeText(this@MainActivity, "Authentication failed", Toast.LENGTH_SHORT).show()
        }
    })

val promptInfo = BiometricPrompt.PromptInfo.Builder()
    .setTitle("Biometric Login")
    .setSubtitle("Log in using your biometric credential")
    .setNegativeButtonText("Cancel")
    .build()

biometricPrompt.authenticate(promptInfo)
Enter fullscreen mode Exit fullscreen mode

iOS Implementation ๐Ÿ

iOS uses the LocalAuthentication framework for biometric authentication. Hereโ€™s how to implement it:

Step 1: Import the Framework

import LocalAuthentication
Enter fullscreen mode Exit fullscreen mode

Step 2: Code Implementation

Hereโ€™s a Swift example:

let context = LAContext()
var error: NSError?

if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
    context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "Log in with biometrics") {
        (success, evaluationError) in

        DispatchQueue.main.async {
            if success {
                print("Authentication successful! ๐Ÿ”‘")
            } else {
                print("Authentication failed: \(evaluationError?.localizedDescription ?? "Unknown error")")
            }
        }
    }
} else {
    print("Biometric authentication not available: \(error?.localizedDescription ?? "Unknown error")")
}
Enter fullscreen mode Exit fullscreen mode

Best Practices ๐Ÿ“

  1. Fallback Options โ€” Always provide an alternative login method, such as a PIN or password.
  2. Data Privacy โ€” Never store raw biometric data; use platform-provided APIs.
  3. User Education โ€” Clearly explain why biometrics are being used and how they enhance security.

Testing Tips ๐ŸŽฎ

  • Use real devices with biometric sensors for accurate testing.
  • Simulate biometric failure scenarios to ensure fallback options work seamlessly.

Conclusion โœจ

Implementing biometric authentication in your Android and iOS apps not only boosts security but also enhances user experience. With the steps outlined above, you can easily integrate this feature into your app.

Happy coding! โ˜•๏ธโš’๏ธ


Letโ€™s Discuss! ๐Ÿ’ฌ

Have you implemented biometric authentication in your apps? Share your experiences, tips, or challenges in the comments below!

Android #iOS #Biometrics #AppDevelopment #Programming ๐ŸŒ

Top comments (0)