# Quickstart: iOS

> Add the Herald SDK to an iOS app, identify a user, track an event, register for push and show content cards.

This guide takes an iOS app from no SDK to a verified first event, a registered push token and the default content card feed. It takes about 30 minutes.

:::prereq
- An app that targets iOS 16 or later, built with Xcode 16 or later.
- A Herald sending connection with an app key. In Herald, open **Settings > Sending**, open the connection and find the **Mobile SDK** card. If it has no key, click **Generate app key**. The card also shows your endpoint URL.
- A backend route that returns a user token for the signed-in user, unless identity verification is **Off**. See [Users and identity](/docs/concepts/users-and-identity#user-tokens-and-verification-modes).
- For push: an APNs auth key on the connection. See [iOS push setup](/docs/push-notifications/ios-setup).
:::

## 1. Add the package

In Xcode, choose **File > Add Package Dependencies**, enter `https://github.com/ender1311/herald-ios`, choose version 0.6.0 or later, and add the `HeraldSDK` and `HeraldUI` libraries to your app target. For a `Package.swift` manifest, [Platform support](/docs/overview/platform-support#install) has the lines.

## 2. Configure the SDK at launch

Call `Herald.configure` once, as early as possible. Debug logging helps while you integrate; remove it before release.

```swift
import SwiftUI
import UserNotifications
import HeraldSDK
import HeraldUI

@main
struct LumenApp: App {
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene { WindowGroup { HomeView() } }
}

final class AppDelegate: NSObject, UIApplicationDelegate, @preconcurrency UNUserNotificationCenterDelegate {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
    ) -> Bool {
        Herald.configure(appKey: "YOUR_APP_KEY", endpoint: URL(string: "https://YOUR_HERALD_HOST")!) { options in
            options.logLevel = .debug
        }
        Herald.useDefaultInAppUI() // default in-app message views
        UNUserNotificationCenter.current().delegate = self
        return true
    }
}
```

Every SDK call returns at once and runs in the background, in the order you made it. Calls made before `configure` are kept and applied once it runs.

## 3. Identify the user

After sign-in, call `identify` with your stable External ID, then `setUserToken` with the token your backend returns. Call them in that order: identifying a different user clears the previous token.

```swift
func didSignIn(userId: String, userToken: String) {
    Herald.shared.identify(userId: userId, traits: ["plan": "free"])
    Herald.shared.setUserToken(userToken)
}
```

On sign-out, call `Herald.shared.reset()`.

## 4. Track an event

```swift
Herald.shared.track("workout_completed", properties: ["minutes": 32, "type": "strength"])
```

Events are queued on the device and sent in batches. [Events and attributes](/docs/concepts/events-and-attributes) lists the rules each call must follow.

## 5. Register for push

Pass the APNs token to the SDK, ask for permission at a moment that makes sense to the user, and forward notification taps.

```swift
extension AppDelegate {
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Herald.shared.setPushToken(deviceToken)
    }

    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void
    ) {
        Herald.shared.handleNotificationResponse(response)
        completionHandler()
    }
}

struct EnablePushButton: View {
    var body: some View {
        Button("Turn on workout reminders") {
            Task { _ = await Herald.shared.requestPushAuthorization() }
        }
    }
}
```

`requestPushAuthorization()` shows the system prompt, registers with APNs when the user allows it, and records the push subscription. [iOS push setup](/docs/push-notifications/ios-setup) covers the APNs key, foreground display and later launches.

## 6. Show content cards

`ContentCardFeed` from `HeraldUI` shows the signed-in user's cards, pinned first and then newest, and logs impressions, clicks and dismissals for you.

```swift
struct HomeView: View {
    var body: some View {
        NavigationStack {
            ContentCardFeed()
                .navigationTitle("For you")
        }
    }
}
```

To draw cards in your own views instead, see [Building a custom UI](/docs/content-cards/custom-ui).

## Verify it works

1. Run the app, sign in and log a workout. Send the app to the background: the SDK sends its queue as the app leaves the foreground.
2. In the Xcode console, filter for the subsystem `com.herald.sdk`. A line that starts with `Herald rejected a batch` means the request failed; anything else at debug level is normal traffic.
3. In Herald, open **Audience > Search users** and look up the External ID. The profile shows the traits you passed to `identify`, such as `plan`.
4. Have an agent send a content card to that user through the same connection. The card appears at the next session start, or right away if you call `Herald.shared.refreshContentCards()`.

## Troubleshooting

- **One `Message sync needs a user token; waiting for setUserToken` debug line right after sign-in.** With verification **Required**, `identify` fetches messages before the token is set. It is harmless: `setUserToken` fetches again. Call `setUserToken` immediately after `identify`. A session-start in-app message can first show in the user's next session. A `Message sync failed with HTTP 401: Herald refused the user token` warning is different: the token the app sent is wrong. See [Error codes](/docs/reference/error-codes).
- **`Herald rejected the app key: check the publishable key in configure`.** The app key is wrong, or the connection is not active. The SDK keeps the events and stops sending until the app launches again; fix the key in `configure`.
- **`Herald needs a user token: call setUserToken`.** Identity verification is **Required** and the signed-in user's token is missing or wrong. The SDK keeps that user's events and sends them after the next `setUserToken`.
- **The profile does not show the traits.** With verification set to **Optional**, a batch without a token is stored as anonymous and writes nothing to the profile. Call `setUserToken` after `identify`.
- **`track ignored: name must not be empty`** or a similar line. The SDK checks every call against the server's limits and drops invalid calls on the device. Fix the call; nothing was sent.
- **No cards.** Cards are fetched for identified users only. An agent send made before the user's device first reached Herald is suppressed, so send again after the app has run once.

## Next

- [Users and identity](/docs/concepts/users-and-identity)
- [iOS push setup](/docs/push-notifications/ios-setup)
- [Content cards](/docs/content-cards/overview)
