📦 SwiftyLaunch Modules
🔗 SharedKit
Using Biometric Authentication (Face ID, Touch ID)

Using Biometric Authentication (Face ID, Touch ID) in your App

SharedKit (SwiftyLaunch Module) - Biometric Authentication

Using biometric authentication can be useful to protect actions that might be seen as sensitive by the user. SwiftyLaunch makes it trivial to add biometric authentication to your app.

First of all, you are required to add a description to your App's Info.plist file. This description will be shown to the user when they are asked to authenticate with Face ID or Touch ID. SwiftyLaunch already includes a default description for you, but you can choose your own.

Biometric Authentication Description in Info.plist

Basic Usage

To handle biometric authentication, SwiftyLaunch provides a convenient BiometricAuth class.

Just call its static authenticate() method to perform an authentication check. It will return a Bool value indicating whether the authentication was successful or not.

💡

This will automatically pick the right biometric authentication method depending on user's device: FaceID for devices with FaceID, and TouchID for devices with TouchID, and will fallback to passcode if none is available or the user has disabled biometric authentication.

import SharedKit
 
func printSecretEmoji() async {
    if (await BiometricAuth.authenticate()) {
        print("😎")
    }
}

Function definition:

BiometricAuth.swift
public class BiometricAuth {
    static public func authenticate() async -> Bool { }
}

Another convenient way to perform biometric authentication is to use the BiometricAuth.executeIfSuccessfulAuth() method. It wraps around the authenticate() method and executes a closure if the authentication was successful.

import SharedKit
 
func printSecretEmoji() async {
    await BiometricAuth.executeIfSuccessfulAuth {
        print("😎") // called on a successful authentication
    } otherwise: {
        print("🤨") // called on a failed authentication
    }
}

Function definition:

BiometricAuth.swift
public class BiometricAuth {
    static public func executeIfSuccessfulAuth(
        _ onSuccessClosure: () -> Void,
        otherwise onFailedClosure: (() -> Void)? = nil
    ) async { }
}

There are also an async versions of the executeIfSuccessfulAuth() method that allows you to pass an async closure to the onSuccessClosure, onFailedClosure or both.

Protecting a SwiftUI View with Face ID or Touch ID

The .sensitiveView() view modifier has an optional protectWithBiometrics parameter that can be set to true to protect the view with biometric authentication. It uses the BiometricAuth class and the previously mentioned executeIfSuccessfulAuth() method to handle the authentication.

The view will be blurred and show a lock icon when its opened or when the app is not in the foreground. When the view becomes active, it will show the biometric authentication prompt and will only show the content if the authentication was successful.

import SwiftUI
import SharedKit
 
struct ContentView: View {
    var body: some View {
        VStack {
            Text("Some Sensitive Information")
        }
        .sensitiveView(protectWithBiometrics: true)
    }
}

SwiftyLaunch's integrated Developer Settings View is automatically protected with biometric authentication out of the box to create a usage example for you:

Sensitive View Modifier with Biometric Authentication Protection