How to enable Face ID for apps on iOS 18

Are you an iOS developer looking to take your app to the next level by incorporating Face ID? Look no further! In this article, we will walk you through the process of enabling Face ID for apps on iOS 18. We’ll also touch on some best practices and common mistakes to avoid while implementing this feature.

Before Diving In

First things first, let’s make sure you have an app that is compatible with Face ID. As of now, Face ID is only available on the iPhone XS, iPhone XS Max, iPhone XR, iPhone X, iPhone 8, iPhone 8 Plus, and iPhone SE. If your app is not currently compatible with Face ID, you’ll need to update it before proceeding.

Next, make sure you have an iOS device running at least version 17.4. This is the minimum version required to support Face ID for apps.

Now that we’ve covered the basics, let’s get started!

Step 1: Create a New Keychain Service

The first step in enabling Face ID for your app is to create a new keychain service. This service will store any sensitive information, such as passwords or credit card numbers, that the user will need to authenticate with Face ID.

  1. Open Xcode and navigate to your project settings.
  2. Click on the “Signing & Capabilities” tab.
  3. Scroll down until you find the “Keychain Services” section.
  4. Click on the “+” button to add a new keychain service.
  5. Enter a name for your keychain service and select the appropriate options (e.g., whether it should be accessible only when the device is unlocked, etc.).
  6. Save your changes.

Step 2: Update Your App’s Info.plist File

Once you’ve created a new keychain service, the next step is to update your app’s Info.plist file to include the necessary Face ID entitlements.

  1. Open Xcode and navigate to your project settings.
  2. Click on the “Entitlements” tab.
  3. Scroll down until you find the “Face ID & Touch ID Code Signing Entitlements” section.
  4. Check the box next to “Keychain Service” to enable it for your app.
  5. Save your changes.

Step 3: Implement Face ID Authentication in Your App

Now that you’ve set up the necessary keychain service and entitlements, it’s time to actually implement Face ID authentication in your app.

  1. Import the AVFoundation framework at the top of your view controller file.
  2. Add a UIView to your view controller’s main storyboard and set its constraints.
  3. Create an instance of AVCaptureSession and configure it to use the device’s camera as its input.
  4. Set up the session’s delegate to receive notifications when Face ID authentication is successful or failed.
  5. Present a UIActivityViewController with the appropriate options to initiate Face ID authentication.
  6. Once authentication is complete, retrieve the user’s stored credentials from the keychain service and use them to authenticate your app.
  7. Save any new credentials that are entered by the user in the keychain service.

Here’s some sample code to get you started:

swift
import AVFoundation
class ViewController: UIViewController, AVCaptureSessionDelegate {

@IBOutlet weak var captureView: UIView!

let session = AVCaptureSession()

override func viewDidLoad() {
    super.viewDidLoad()

    // Configure the session
    let device = AVCaptureDevice.default(for: .video)!
    let input = try! AVCaptureDeviceInput(device: device)
    session.addInput(input)

    // Set up the delegate
    session.delegate = self

    // Present the activity view controller
    let activityViewController = UIActivityViewController(activityItems: nil, applicationActivities: nil)
    activityViewController.presentationContext = self
}

func captureSession(_ session: AVCaptureSession, didUpdate status: AVCaptureSession.Status) {
    if status == .ready {
        // Set up the capture preview layer
        let capturePreviewLayer = AVCaptureVideoPreviewLayer(session: session)
        capturePreviewLayer.frame = view.bounds

        // Add the preview layer to the view
        captureView.layer.addSublayer(capturePreviewLayer)

        // Start capturing video
        session.startRunning()
    } else if status == .failed {
        print("Capture session failed: (status.localizedDescription)")
    }
}

func captureSession(_ session: AVCaptureSession, didReceive message: AVCaptureMessage, from connection: AVCaptureConnection) {
    guard let messageType = message.type as? AVCaptureDeviceInput.DeviceMetadataOutput.MessageType,
          messageType == .faceIDAuthenticationAvailable else { return }

    // Present the Face ID authentication view controller
    let faceIDViewController = UIActivityViewController(activityItems: nil, applicationActivities: nil)
    faceIDViewController.presentationContext = self
}

func captureSession(_ session: AVCaptureSession, didReceive message: AVCaptureDeviceInput.DeviceMetadataOutput.MessageType, from connection: AVCaptureConnection) {

Here's some sample code to get you started
guard let messageType = message.type as? AVCaptureDeviceInput.DeviceMetadataOutput.MessageType,
messageType == .faceIDAuthenticationSucceeded else { return }

    // Retrieve the user's stored credentials from the keychain service
    let query = ["service": "com.example.mykeychainservice"] as [String: Any]
    let options: [kCFQueryPersistentStorageSecureContainerOptionsKey: Any?] = [kCFQueryPersistentStorageSecureContainerOptionsKey: kCFBooleanTrue]

    if let data = try? Data(contentsOf: query as NSDictionary, options: options) {
        // Use the data to authenticate your app
    } else {
        print("Failed to retrieve credentials from keychain service")
    }
}

func captureSession(_ session: AVCaptureSession, didReceive message: AVCaptureDeviceInput.DeviceMetadataOutput.MessageType, from connection: AVCaptureConnection) {
    guard let messageType = message.type as? AVCaptureDeviceInput.DeviceMetadataOutput.MessageType,
          messageType == .faceIDAuthenticationFailed else { return }

    print("Face ID authentication failed: (message.localizedDescription)")
}

}

Step 4: Test Your App with Face ID

Now that you’ve implemented Face ID authentication in your app, it’s time to test it on a device that supports Face ID.