# Get started with iOS

> Build a small SwiftUI app that enrols your face and verifies it offline, on your own iPhone.

Source: https://docs.faceping.ai/start/ios/

What you'll build

### A face check that works in airplane mode

An app that enrols your face, verifies it live with a head-turn liveness check, and still matches with no network.

About 10 minutes
iPhone
Free sandbox key

## Get a sandbox key

Sign up at [app.faceping.ai/signup](https://app.faceping.ai/signup?platform=ios) with your email, then open [**Developers → Sandbox keys → Create sandbox key**](https://app.faceping.ai/developers). It starts with `fp_test_`. It also works on the API for your practice list, so keep test apps within your team, and revoke the key under Developers if it leaks.

The sandbox is free and instant: up to 25 faces per device, each deleted 24 hours after it was enrolled. No card, no approval.

## Create the project

You need a Mac with Xcode 26 or later, and an iPhone (iOS 15.1 or later) with a cable.

In Xcode: **File → New → Project → iOS → App**. Call it `FaceCheck`, with **SwiftUI** as the interface and **Swift** as the language.

A new project targets the newest iOS only, so set it to match FacePing: select the FaceCheck target, open **General**, and under **Minimum Deployments** set iOS to **15.1**. (Xcode's default is the newest iOS, which your iPhone may not have yet.)

First iPhone app on this Mac?

The 10 minutes assume Xcode already runs apps on your iPhone. If it doesn't, set that up first:

- Install [Xcode](https://developer.apple.com/xcode/) 26 or later and sign in with your Apple ID (**Xcode → Settings → Apple Accounts**). A free Apple ID is enough to run on your own phone.
- On the iPhone, turn on **Developer Mode** (**Settings → Privacy & Security → Developer Mode**) when Xcode asks.

## Add FacePing

One Swift package. It brings the face models, liveness checks and a camera view with it.

In Xcode: **File → Add Package Dependencies…**, and enter:

```text
https://github.com/faceping/faceping-ios
```

FacePing for iOS is in preview, so set the dependency rule to **Exact Version** `1.0.0-preview.5` (Xcode only offers previews when you type one in). Add the `FacePing` library to the FaceCheck target. Xcode downloads the package once (about 45 MB: the FacePing engine, Microsoft's ONNX Runtime and the face models). It adds about 32 MB to your app.

## Allow the camera

iOS asks the person before the camera turns on, and shows them your reason. Select the FaceCheck target, open the **Info** tab, click **+** and add **Privacy - Camera Usage Description** with your reason. (New projects have no Info.plist file: Xcode builds it from this tab. If your project does have one, add the key there.)

```xml title="Info.plist"
<key>NSCameraUsageDescription</key>
<string>FaceCheck uses the camera to verify your face.</string>
```

Without it, iOS stops the app the moment the camera starts.

## Turn FacePing on

Set FacePing up once, when the app starts, with your sandbox key.

```swift title="FaceCheckApp.swift"
import FacePing
import SwiftUI

@main
struct FaceCheckApp: App {
    init() {
        // Starts loading the face models in the background, so the first check is quick.
        do {
            try FacePing.configure(sandboxKey: "fp_test_…")
        } catch {
            print("FacePing setup: \(error)")
        }
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
```

Nothing to set up for backups: FacePing keeps its face data out of iCloud and computer backups itself, and its key stays in this iPhone's Keychain.

## Add the screen

A camera view and two buttons: one enrols your face on the phone, the other verifies it. Replace `ContentView.swift`. (`import Combine` is for `ObservableObject`: new Xcode projects need it imported by name.)

```swift title="ContentView.swift"
import Combine
import FacePing
import SwiftUI

@MainActor
final class CheckModel: ObservableObject {
    let camera = FaceCameraView()
    @Published var status = ""

    func enroll() async {
        guard let faceping = FacePing.shared else { return notSetUp() }
        do {
            let photo = try await camera.capture()
            let result = try await faceping.enroll(id: "me", photo: photo)
            status = result.isEnrolled ? "Enrolled. Now tap Verify." : "Try again: \(result.outcome)"
        } catch {
            // For example no connection on the very first run (the sandbox key is activated once), or no camera.
            status = error.localizedDescription
        }
    }

    func verify() async {
        guard let faceping = FacePing.shared else { return notSetUp() }
        do {
            // A live check: matches your face, then asks you to turn your head (liveness).
            let result = try await faceping.verifyLive(id: "me", camera: camera)
            let time = String(format: "%.2f s", result.elapsed)
            status = result.isMatch ? "Match · \(time)" : "Try again: \(result.outcome)"
        } catch {
            status = error.localizedDescription
        }
    }

    // FacePing.configure failed in FaceCheckApp, for example a mistyped key.
    private func notSetUp() {
        status = "FacePing isn't set up: check the sandbox key in FaceCheckApp.swift."
    }
}

struct ContentView: View {
    @StateObject private var model = CheckModel()

    var body: some View {
        VStack(spacing: 16) {
            // Shows why if the camera can't start, for example when the person refused it.
            FaceCamera(model.camera) { model.status = $0 }
                .frame(height: 420)
            Button { Task { await model.enroll() } } label: {
                Text("Enrol my face").frame(maxWidth: .infinity)
            }
            .buttonStyle(.bordered)
            .controlSize(.large)
            Button { Task { await model.verify() } } label: {
                Text("Verify").frame(maxWidth: .infinity)
            }
            .buttonStyle(.borderedProminent)
            .controlSize(.large)
            Text(model.status)
                .font(.title3)
        }
        .padding(24)
    }
}
```

## Run it on your phone

Plug your iPhone in and choose it as the run destination. The simulator has no front camera, so the face checks need a real iPhone.

1. Select the FaceCheck target, open **Signing & Capabilities** and pick your **Team** (add your Apple ID in **Xcode → Settings → Apple Accounts** if it isn't listed).
2. Turn on **Developer Mode** on the iPhone if it asks (**Settings → Privacy & Security → Developer Mode**).
3. Press **Run**, and allow the camera when asked.

**Run** builds in Debug, which is fine, timings included: FacePing comes as a prebuilt, optimised framework, so its checks run at full speed in either configuration.

The first run needs a connection, to activate your sandbox key. Tap **Enrol my face**, then **Verify**: you should see `Match`.

**Now switch on airplane mode and tap Verify again.** Still a match: the check ran entirely on your phone.

## You've built a face check that works offline

Next, make it real: verify the people in your app (members, visitors or people with tickets), and apply to go live when you're ready.

- [Clone the finished app](https://github.com/faceping/ios-starter): github.com/faceping/ios-starter
- [How the SDK works](https://docs.faceping.ai/sdk/): Liveness, storage, sandbox limits
- [iOS reference](https://docs.faceping.ai/sdk/ios/): Every type and method
- [Go to production](https://docs.faceping.ai/going-live/): Apply and sync faces to devices

## Try a check-in device on your practice list

This is the ticketing flow, end to end, in the sandbox: a person enrols through your server or an enrolment link, and a check-in device verifies them offline.

1. Create your practice list, if your account doesn't have one: **New list** in the [dashboard](https://app.faceping.ai/lists) asks for everything it needs, or use [quickstart steps 1 and 2](https://docs.faceping.ai/quickstart/#1-create-a-check-in-list) (create the list, then set its widget details: enrolment links need them). Then enrol someone on it with your sandbox key: open an enrolment link ([quickstart step 3](https://docs.faceping.ai/quickstart/#3-create-an-enrolment-link-for-a-person); links last 30 minutes unless you pass `validForHours`), or enrol a selfie from your server with the copy-paste request in [Enrol someone from your server](https://docs.faceping.ai/quickstart/#enrol-someone-from-your-server). Note their `personId`, for example `ACME-000123` (if your team shares the practice list, pick your own).
2. Add a check-in device: the list's **Devices** tab in the [dashboard](https://app.faceping.ai/lists), or `POST /v1/lists/{listId}/devices`. Copy the `fpd_` device token: it's shown once.
3. Check the token works before you build: `curl https://api-eu.faceping.ai/v1/device -H "X-Device-Token: fpd_…"` returns the list's name and `peopleEnrolled`. It doesn't download any faces or count as a sync.
4. Make a device build that sits next to your sandbox app: in Finder, copy the whole `FaceCheck` project folder to `FaceCheck-device` and open the copy. (Duplicating the target instead would share the same Swift files, so your edits would change the sandbox app too.) In the copy, select the FaceCheck target and give it its own **Bundle Identifier** (**Signing & Capabilities**, for example `com.yourcompany.facecheck.device`) and **Display Name** (**General**, for example `FaceCheck Device`).
5. In the copy's `FaceCheckApp.swift`, set FacePing up with the device token instead of your sandbox key:

```swift title="FaceCheckApp.swift (device build)"
try FacePing.configure(deviceToken: "fpd_…")
```

6. Replace the copy's `ContentView.swift`. It verifies the person you enrolled, syncs when the screen appears and shows every later sync. People enrol on the server, not in the app (`enroll` throws `FacePingError.invalidOperation` on a device), so there's no enrol button:

```swift title="ContentView.swift (device build)"
import Combine
import FacePing
import SwiftUI

@MainActor
final class CheckModel: ObservableObject {
    let camera = FaceCameraView()
    let personId = "ACME-000123"   // the person you enrolled
    @Published var status = ""

    // Syncs now, then shows every later sync (FacePing syncs again every few minutes).
    func watchSync() async {
        guard let faceping = FacePing.shared else {
            status = "FacePing isn't set up: check the device token in FaceCheckApp.swift."
            return
        }
        do {
            show(try await faceping.sync())
        } catch {
            status = error.localizedDescription
        }
        for await r in faceping.synced { show(r) }
    }

    func verify() async {
        guard let faceping = FacePing.shared else { return }
        do {
            let result = try await faceping.verifyLive(id: personId, camera: camera)
            let time = String(format: "%.2f s", result.elapsed)
            switch result.outcome {
            case .match: status = "Match · \(time)"
            case .notEnrolled: status = "\(personId) isn't on this device yet: enrol them, then wait for the next sync."
            default: status = "Try again: \(result.outcome)"
            }
        } catch {
            status = error.localizedDescription
        }
    }

    private func show(_ r: SyncResult) {
        status = "\(r.enrolled) people synced (\(r.status))"
    }
}

struct ContentView: View {
    @StateObject private var model = CheckModel()

    var body: some View {
        VStack(spacing: 16) {
            FaceCamera(model.camera) { model.status = $0 }
                .frame(height: 420)
            Button { Task { await model.verify() } } label: {
                Text("Verify").frame(maxWidth: .infinity)
            }
            .buttonStyle(.borderedProminent)
            .controlSize(.large)
            Text(model.status)
                .font(.title3)
        }
        .padding(24)
        .task { await model.watchSync() }
    }
}
```

7. Run the device build. It downloads the list's faces at start-up and every few minutes after; the status line shows how many people synced and the sync status, and updates with each sync. `revoked` means the token was revoked, mistyped or its list deleted, and the device wipes its faces. Then tap **Verify**, with no network if you like.

Each face stays on the device no longer than its 24 hours on the practice list. See [Devices and offline sync](https://docs.faceping.ai/devices/).

If you switch one install from the sandbox key to a device token instead, the two sets of faces stay apart: the app sees only the people synced from the device's list, and your sandbox faces come back (if they're under 24 hours old) when you switch back. Uninstalling removes both.

The [finished app](https://github.com/faceping/ios-starter) runs as a check-in device too: in `Config/Local.xcconfig`, set `FACEPING_DEVICE_TOKEN = fpd_…` and `FACEPING_PERSON_ID = ACME-000123` (a device token, if set, is used instead of the sandbox key).
