Get started with the iOS Swift SDK

For iOS, macOS, tvOS, watchOS, and visionOS developers10 minBeginner

Install the iOS Swift SDK (AmplitudeSwift), initialize it, and send one custom event to Amplitude. Each step lists the problems that commonly break that step, so you can fix a problem where it happens. For the full option list, go to Configuration options. For changes between versions, go to the iOS Swift SDK release notes.

The iOS Swift SDK is the analytics-only package. If you also need Session Replay or Feature Experiment, install the Unified SDK for Swift (AmplitudeUnified) instead. It includes AmplitudeSwift, so don't add both packages to the same app. To compare the packages, go to Choose an iOS installation.

Prerequisites

  • An Amplitude project and its API key. Find the key in Settings > Projects > your project > General.
  • The data center your organization uses: US (default) or EU.
  • The latest SDK release, or a pinned version you check against the release notes for the features you use.
  • Xcode 16.3 or later (Swift 6.1 or later). The SDK source uses Swift 6.1 syntax, and the SDK's release builds use Xcode 16.3.
  • A deployment target of at least iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 7.0, or visionOS 1.0.

Install the SDK

Choose one installation method. Swift Package Manager is built into Xcode and needs no extra tools.

  1. In Xcode, select File > Add Package Dependencies.
  2. Enter https://github.com/amplitude/Amplitude-Swift in the search field.
  3. Set Dependency Rule to Up to Next Major Version, starting from 1.19.4.
  4. Select Add Package, then add the AmplitudeSwift product to your app target.

To add the SDK to a Package.swift manifest instead:

swift
dependencies: [
    .package(url: "https://github.com/amplitude/Amplitude-Swift", from: "1.19.4"),
],
targets: [
    .target(
        name: "MyApp",
        dependencies: [.product(name: "AmplitudeSwift", package: "Amplitude-Swift")]
    ),
]

Fix installation problems

Initialize the SDK

Create one Amplitude instance when your app launches, and keep a strong reference to it. For example, store the instance as a property of your App struct or AppDelegate, or in a shared singleton. Use that instance for every call.

swift
import AmplitudeSwift

let amplitude = Amplitude(configuration: Configuration(
    apiKey: "YOUR_API_KEY"
))

If your project uses the EU data center, set serverZone:

swift
let amplitude = Amplitude(configuration: Configuration(
    apiKey: "YOUR_API_KEY",
    serverZone: .EU
))

By default, the SDK tracks sessions (session_start and session_end events) and nothing else automatically. To add app lifecycle events, screen views, element interactions, network requests, or frustration interactions, go to Autocapture.

Swift requires Configuration arguments in their declared order. If Xcode reports an argument order error, check the order in Configuration options.

Fix initialization problems

Send a first event

Track one custom event, then call flush() to upload it right away. To see the result, pass a callback to Configuration. The SDK calls the callback once for each event after the upload finishes.

swift
let amplitude = Amplitude(configuration: Configuration(
    apiKey: "YOUR_API_KEY",
    callback: { event, code, message in
        print(event.eventType, code, message)
    }
))

amplitude.track(
    eventType: "Sign Up Clicked",
    eventProperties: ["plan": "starter"]
)
amplitude.flush()
// Sign Up Clicked 200 Successfully send event

Without flush(), the SDK uploads queued events every 30 seconds (flushIntervalMillis), when the queue reaches 30 events (flushQueueSize), or when the app moves to the background. Remove the flush() call after you confirm the setup.

The callback fires only for responses 200, 400, and 413. For other failures, such as rate limits, server errors, or no network, the SDK keeps the event, retries, and logs an error instead.

Fix upload problems

Confirm the event in Amplitude

  1. In Amplitude, open User Lookup and search for the device ID from amplitude.getDeviceId(), or for the user ID if you set one.
  2. Open the Activity tab and turn on Live event updates.
  3. Confirm that Sign Up Clicked appears with the plan property.

If the event doesn't appear within a minute, follow Verify events. That page walks through each stage between track() and Amplitude's servers.

Next steps

Was this helpful?

For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

On this page