---
title: "Togo for iOS"
description: "Every method and option of the iOS SDK, the Swift package for iPhone and iPad apps."
canonical: "https://www-stage.togohq.ai/docs/reference/sdk-ios"
applies_to: "sdk-ios 1.x"
---

# Togo for iOS

Every method and option of the iOS SDK, the Swift package for iPhone and iPad apps.

```swift
dependencies: [
  .package(url: "https://github.com/togohq/sdk-ios", from: "1.0.0")
]
```

iOS 15 or later, with no dependencies. Add the **Togo** library to your app target, from Xcode's
**File → Add Package Dependencies…** or in `Package.swift`. Every method is a static method on
`Togo`, can be called from any thread, and never waits on the network. Calls run in the order you
made them. No method throws: a refused call logs one `[togo] …` line saying why and reaches
[`onError`](#onerror). A guide to using it: [Send events from an iOS app](/docs/ios).

## `Togo.setup(_:)`

```swift
import Togo

#if DEBUG
  let key = "togo_pkt_…"
#else
  let key = "togo_pkl_…"
#endif
Togo.setup(TogoConfig(writeKey: key))
```

Call it once, as early as the app allows: in `App.init`, or in your app delegate as the app finishes
launching. A second call is ignored and logged; the first configuration stands. A call made before
`setup` is dropped, with one log line. Use the test key in debug builds, so `verify_tracking` sees
your simulator's events.

`setup` sends the launch's `$app_installed` or `$app_updated`. It starts no session: the app coming
to the foreground does, with `$app_opened`. On a server, the SDK disables itself with one warning
and stores and sends nothing — use the [Node.js](/docs/reference/sdk-node) or
[.NET](/docs/reference/sdk-dotnet) SDK there.

## Options

| Option                   | Default                | What it does                                                                                       |
| ------------------------ | ---------------------- | -------------------------------------------------------------------------------------------------- |
| `writeKey`               | —                      | Required. `togo_pkt_…` (test) or `togo_pkl_…` (live). A malformed key sends nothing, with an error |
| `endpoint`               | `https://in.togohq.ai` | Where events are sent                                                                              |
| `sessionTimeout`         | `1800` (30 min)        | Seconds of inactivity that end a session. `0` or less turns sessions off: no `sessionId` is sent   |
| `consent`                | `.notRequired`         | The starting consent state. `.pending` holds events in memory until `setConsent(.granted)`         |
| `onDenied`               | `.drop`                | What happens once consent is denied: held events are dropped                                       |
| `consentBufferMaxEvents` | `100`                  | Events held while consent is `.pending`; past it, the oldest are dropped and reported              |
| `defaultProperties`      | `[:]`                  | Properties on every `track()` event, such as `["source": "ios_app"]`                               |
| `appVersion`             | the bundle             | Your release number, read as `$app_version`. Set it when `CFBundleShortVersionString` is not it    |
| `appBuild`               | the bundle             | Your build number, read as `$app_build`. Set it when `CFBundleVersion` is not it                   |
| `onError`                | —                      | Called for every event, or part of one, that will not be recorded. See [`onError`](#onerror)       |

A session also ends after 24 hours, whatever the timeout. A batch is 50 events or 5 seconds,
whichever comes first, fixed on a device; to send at once, call `Togo.flush()`.

Options go in `TogoConfig(writeKey:…)`; every one but `writeKey` has its default.
`defaultProperties` is fixed at `setup`. An event's own value for the same key wins, and the
collision is logged once.

## `Togo.track(_:properties:)`

```swift
Togo.track("plan_selected", properties: ["plan_id": "pro", "seats": 5])
```

A property value is a string, an integer, a floating-point number, a boolean or `nil`, written as a
literal or as a `TogoValue`. The name is stored exactly as you send it, and is 1 to 255 characters.
A name starting with `$` is Togo's: with a live key the event is dropped and reported; with a test
key it is sent, so `verify_tracking` reports it with its fix. A property name starting with `$` is
reserved ([Reserved properties](/docs/reference/reserved-properties)), and a name longer than 255
characters, or holding a control character or an email address, is one Togo cannot use: with a live
key the SDK drops the property and sends the rest; with a test key it sends it as well, so
`verify_tracking` reports it with its fix. Both are reported through [`onError`](#onerror).

A string value longer than 1,024 characters is cut to its first 1,024, and past 256 properties the
first 256 in sort order by name are kept. The event is still sent, and both are reported through
[`onError`](#onerror); with a test key the value or the properties are sent whole, so
`verify_tracking` shows what Togo keeps. Never put an email address, a name or text a person typed
in an event property: they belong in `setPersonProperties`.

## `Togo.identify(_:)`

```swift
Togo.identify(account.id)
```

Call it at sign-in with your own account id — the same one your website and server use, never an
email address. The same id again does nothing; a different id first does what `reset()` does. An
empty id, or one over 128 characters, is refused as `user_id_invalid`.
[Identify people](/docs/guides/identify) explains how one person is followed across platforms.

## `Togo.setPersonProperties(_:)`

```swift
Togo.setPersonProperties(
  email: account.email,
  name: account.name,
  set: ["plan": "pro"]
)
```

`email` and `name` set the reserved person properties `$email` and `$name`; `set`, `setOnce` and
`unset` take your own keys, as in [Set person properties](/docs/guides/person-properties). An
argument you leave out changes nothing, and `.some(nil)` removes the value:

```swift
Togo.setPersonProperties(email: .some(nil))
```

Set the email and name there, never as an `email` or `name` key in `set`. The same update can be
built as one `PersonPropertyUpdate` and passed on its own:

```swift
let update = PersonPropertyUpdate(email: account.email)
Togo.setPersonProperties(update)
```

## `Togo.reset()`

Call it at logout. The device gets a new anonymous visitor and a new session; the device id stays.

## `Togo.setConsent(_:)` and `Togo.getConsent()`

```swift
Togo.setConsent(.granted) // or .denied
```

Four states: `.notRequired`, `.pending`, `.granted` and `.denied`. `setConsent` can be called at any
time and as often as you like. `.granted` after `.pending` sends what was held; `.denied` drops it
and deletes everything the SDK stored on the device. `getConsent()` returns the current state.
[Connect a consent banner](/docs/guides/consent) explains the four states.

## `Togo.getAnonymousId()`, `Togo.getDeviceId()`, `Togo.getSessionId()`

Each returns the current id, or `nil` before the first event consent permits; `getSessionId()` is
also `nil` while there is no session. They never create an id. Pass `getDeviceId()` to
`verify_tracking` as `filter.deviceId` to check the events your simulator sent.

## `Togo.flush(completion:)`

```swift
Togo.flush { print("sent") }
```

Sends what is queued now. `completion` runs after the attempt, off the SDK's queue, so it may call
any `Togo` method. An event that could not be sent stays queued and is retried.

## `onError`

```swift
Togo.setup(TogoConfig(writeKey: key, onError: { error in
  SentrySDK.capture(message: error.message)
}))
```

Pass it to the error tracker your app already uses. It is called once for every event that will not
be recorded, or value dropped or cut from one, and once when the client stops sending — in
production as well as in debug builds. Never for consent `.denied`. It runs off the SDK's queue,
after the call that caused it has returned. It receives a `TogoError`: `code`, `message` (what the
log shows, without `[togo] `), `eventIds`, `eventName` and `property` (the property's name when the
report is about one, never its value). The codes are the
[browser SDK's](/docs/reference/sdk-browser#onerror), in `TogoErrorCode`, plus the ones below. Treat
a code you do not recognise as a lost event.

Three codes mean something specific on a device:

| `code`               | What was lost                                                                                                  |
| -------------------- | -------------------------------------------------------------------------------------------------------------- |
| `user_id_invalid`    | Nothing: `identify()` was given an empty id or one over 128 characters, and the user is unchanged              |
| `write_key_rejected` | Nothing yet: Togo refused the key, so the SDK stops sending until the app restarts and keeps the queued events |
| `endpoint_invalid`   | Nothing yet: App Transport Security blocks `endpoint`, or it is not a URL. The SDK stops and keeps the queue   |

## Delivery

Events wait on the device while it is offline and are sent when it reconnects, even after iOS ends
the app; each keeps the time it happened and the same event id across attempts. How many wait, and
for how long: [Limits](/docs/reference/limits).
