> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trevosdk.com/llms.txt
> Use this file to discover all available pages before exploring further.

# iOS

> Experiments and tracking for Swift and SwiftUI apps, assigning the same variants as the web.

`TrevoSDK` runs experiments and records events in iOS apps, assigning the same variant the web
SDK does for the same user. Swift, no dependencies, iOS 15 and later (also visionOS and macOS).
On tvOS it works, but tvOS only lets an app keep files in a cache it may clear, so a device can
lose its anonymous id.

In Xcode, choose **File → Add Package Dependencies** and enter
`https://github.com/trevosdk/trevosdk-ios`. In a `Package.swift`:

```swift theme={null}
.package(url: "https://github.com/trevosdk/trevosdk-ios", from: "0.1.3"),
// in your target's dependencies:
.product(name: "TrevoSDK", package: "trevosdk-ios"),
```

## Create the client

Use a **Mobile** key (`tsk_live_…`). Keep it in your build configuration, for example an
xcconfig value exposed in Info.plist, and create one client for the app's lifetime:

```swift theme={null}
import TrevoSDK

let trevo = TrevoClient(
    options: TrevoOptions(
        apiKey: Bundle.main.object(forInfoDictionaryKey: "TrevoApiKey") as? String ?? ""))
```

The client reads the build's version from `CFBundleShortVersionString`, so `minAppVersion`
gating and the version on every event work with no setup.

## Read a variant

`getVariant` is synchronous and safe on the main thread. It returns `control` until stored
state and the first config fetch have loaded, so wait for `ready()` before showing experiment
UI, the way you would wait for a signed-in user:

```swift theme={null}
struct CheckoutButton: View {
    @State private var variant: String?

    var body: some View {
        Group {
            if variant == "treatment" {
                Button("Start free trial") { trevo.track("checkout_started") }
            } else if variant != nil {
                Button("Subscribe") { trevo.track("checkout_started") }
            }
        }
        .task {
            await trevo.ready()
            variant = trevo.getVariant("checkout-v2")
        }
    }
}
```

Each call records an **exposure** the first time. Pass `trackExposure: false` to read without
recording one. `onChange` calls a listener on the main thread whenever config or identity
changes; keep the returned `TrevoSubscription` and `cancel()` it when the view goes away.

## Parameters

A [parameter](https://docs.trevosdk.com/concepts/parameters.md) lets later experiments change a value with no release:

```swift theme={null}
let headline = trevo.getParameter("paywall.headline", default: "Subscribe")
let trialDays = trevo.getParameter("paywall.trial_days", default: 7)
```

There are overloads for `String`, `Bool`, `Int` and `Double`. A value of another type, or a
number that isn't whole for an `Int`, serves the default.

A `String` parameter can carry [text per language](https://docs.trevosdk.com/concepts/parameters.md#lists-and-translated-text):
pass your app's own translation as the default, and if users can pick the app's language, call
`trevo.setLocale("pt-BR")` when they do. Without it the SDK reads the localization the app runs in.

## Track events and identify users

```swift theme={null}
trevo.track("checkout_completed", properties: ["plan": "pro", "seats": 3])

trevo.identify(user.id)   // after sign-in, and on launch when auth restores a user
trevo.reset()             // on sign-out: clears the user and starts a new anonymous id
```

Events are queued on disk and sent in batches every 15 seconds, when the app moves to the
background, and on `await trevo.flush()`. See [Mobile experiments](https://docs.trevosdk.com/concepts/mobile-experiments.md)
for how experiments ramp on mobile, how the SDK survives a lost network, and the App Store and
Google Play answers. The package ships its own privacy manifest.

## Options

| Option | Default | Notes |
| - | - | - |
| `apiKey` | | Required. A Mobile key, `tsk_live_…` |
| `appVersion` | `CFBundleShortVersionString` | Sent on every event; compared with `minAppVersion` |
| `bootstrapConfig` | `[]` | Assign before storage and the network answer |
| `pollInterval` | 300 s | Config is also refreshed on every foreground |
| `flushInterval` | 15 s | Also flushes when the app moves to the background |
| `maxBatchSize` | 50 | Capped at 500 by the server |
| `configURL`, `ingestionURL` | Trevo | Endpoint overrides for proxied installs |
| `storage`, `transport`, `lifecycle` | Files, URLSession, UIKit | Replace for tests or your own stack |
| `onError` | Unified log | Background failures; the SDK never throws |

## Correctness

The package runs the shared conformance vectors in CI, so a change that would assign a user
differently from the web fails the build. The rules are in the
[bucketing spec](https://docs.trevosdk.com/reference/bucketing-spec.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.