# Swift apps

The Swift package: its calls for screens, events and signed-in people, and its options for test builds, storage and consent.

The [Swift package](/docs/install/swift) counts iPhone and iPad apps. This page covers its calls for screens, events, signed-in people and what opened the app, and its options for test builds, storage and consent.

## Screens

A view counts when the screen changes. The same screen twice in a row counts once, and going back to a screen counts it again, as the back button does on the web. Coming back from the background counts no view.

In SwiftUI, `screen` counts a view each time the screen appears.

```swift
ProductDetail(product: product)
  .screen("Product")
```

In UIKit, or from anywhere else, call `Ultanimus.screen`.

```swift
Ultanimus.screen("Product")
```

Name the kind of screen, such as `Product`, and leave IDs out of the name. Screens then group on the dashboard, and IDs never leave the phone.

## Send an event

```swift
Ultanimus.track("add_to_cart", ["sku": "lamp", "quantity": 2])
```

Property values are strings, numbers or booleans. [Custom events](/docs/tracking/custom-events) covers naming and properties, and the same events work in goals and funnels.

## Signed-in users

Call `identify` with your own ID for the signed-in person, and with `nil` on sign out. They then count as one visitor across days and devices, on the web and in the app.

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

Use an ID from your database, never an email address or a name. [Signed-in users](/docs/tracking/identify) explains what is stored.

## What opened the app

A link that opens the app with `utm_source`, `utm_medium`, `utm_campaign` or `ref` in its address names the visit's source, as it does on a web page. Pass each link your app receives to `opened`.

```swift
ContentView()
  .onOpenURL { Ultanimus.opened($0) }
```

In UIKit, pass the links from your scene delegate's `scene(_:openURLContexts:)`. For anything else that opens the app, such as a notification, call `opened` with a source before the next screen shows.

```swift
Ultanimus.opened(source: "push", campaign: "black-friday")
```

A visit with neither shows as Direct. Installs from the App Store carry no source yet.

## Test builds

Debug builds never count and show under [Test events](/docs/mobile/development#test-events). Pass `debug: true` to log every event to Xcode's console.

TestFlight builds are release builds, so they count like the App Store app. Pass `development: true` in those builds to send them as test events instead. `development: false` counts a debug build, for a quick check of the dashboard.

```swift
Ultanimus.start(site: "YOUR_SITE_ID", development: true)
```

## Storage

The package stores nothing on the phone by default, so each cold start counts as a new visitor. To count a person once a day across restarts, pass `storage`. Storing data on a device can need consent where you ship, so check before you turn it on.

```swift
Ultanimus.start(site: "YOUR_SITE_ID", storage: .standard)
```

Any `UserDefaults` works, such as the one your app group shares. [Visitors and offline](/docs/mobile/visitors-and-offline) explains what it keeps.

## Turn it off

Pass `enabled: false` until a person agrees to analytics, and call `start` again when their choice changes. While it is off the package queues and sends nothing.

```swift
Ultanimus.start(site: "YOUR_SITE_ID", enabled: consent)
```

## Other Apple platforms

The package counts iPhone and iPad apps. It also builds for macOS, Mac Catalyst, tvOS, watchOS and visionOS, so a shared target compiles, and it sends nothing there.
