# Swift

Add the Swift package in Xcode, start it once and name your screens in SwiftUI or UIKit.

The Swift package counts iPhone and iPad apps built with SwiftUI or UIKit. It is plain Swift with no dependencies. It needs iOS 15 and Xcode 16, or newer. [Mobile apps](/docs/mobile) explains how apps count.

## Add the app to a site

An app counts under a site, the same way a website does.

- **The app goes with a website.** Open that site's settings and add the app's bundle ID under Apps, such as `com.acme.app`.
- **An app with no website.** In Add a site, pick An app only and enter the app's bundle ID.

[Apps and sites](/docs/mobile#apps-and-sites) covers separate iOS and Android IDs and app variants.

## Install the package

In Xcode, choose File, then Add Package Dependencies, and paste the address:

```
https://github.com/ultanimus/ultanimus-swift
```

In a `Package.swift`, add it to `dependencies` and to your target:

```swift
.package(url: "https://github.com/ultanimus/ultanimus-swift", from: "0.1.0")
```

```swift
.product(name: "Ultanimus", package: "ultanimus-swift")
```

## Start it

Call `start` once when the app launches, with the site ID from your site's settings. It reads the bundle ID and version from your app.

### SwiftUI

`AcmeApp.swift`

```swift
import SwiftUI
import Ultanimus

@main
struct AcmeApp: App {
  init() {
    Ultanimus.start(site: "YOUR_SITE_ID")
  }

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

### UIKit

`AppDelegate.swift`

```swift
import UIKit
import Ultanimus

@main
final class AppDelegate: UIResponder, UIApplicationDelegate {
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    Ultanimus.start(site: "YOUR_SITE_ID")
    return true
  }
}
```

## Name your screens

In SwiftUI, add `screen` to each screen's view.

```swift
struct Checkout: View {
  var body: some View {
    CheckoutForm()
      .screen("Checkout")
  }
}
```

In UIKit, call `Ultanimus.screen` in `viewDidAppear`.

```swift
override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)
  Ultanimus.screen("Checkout")
}
```

[Swift apps](/docs/mobile/swift) covers what counts as a view and how to name screens.

## Send an event

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

## Check that it works

Run the app from Xcode and move between a few screens. Each screen and event shows under Test events in the site's settings within a few seconds. Debug builds never count, so the dashboard stays empty until a release build sends its first event. [Development builds](/docs/mobile/development) explains test builds and staging.

## Next steps

- [Swift apps](/docs/mobile/swift): signed-in people, what opened the app, storage and consent.
- [Visitors and offline](/docs/mobile/visitors-and-offline): how apps count visitors and send without a connection.
- [App Store and Play privacy](/docs/mobile/store-privacy): what to answer on the App Store's form.
