Get started with the Unified SDK for Swift

이 페이지는 아직 귀하의 언어로 번역되지 않았습니다. 현재 작업 중이므로 곧 다시 확인해 주십시오.

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

This guide shows how to install the Unified SDK for Swift (AmplitudeUnified), initialize every product it includes with one initializer, and send one custom event to Amplitude. For the full option list, go to AnalyticsConfig. For changes between versions, go to the AmplitudeUnified changelog.

The Unified SDK bundles the iOS Swift SDK (AmplitudeSwift) with Feature Experiment and, on iOS only, Session Replay. Every analytics method works the same as in the iOS Swift SDK. If you need Analytics only, install the iOS Swift SDK instead. 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. To find the key, go to Settings > Projects, select your project, and open General.
  • The data center your organization uses: US (default) or EU.
  • Xcode 16.3 or later, which the bundled iOS Swift SDK requires.
  • A deployment target of at least iOS 15.0, macOS 12, tvOS 15, watchOS 9, or visionOS 1.0 when you install with Swift Package Manager in Xcode 27 or later (Swift 6.4). Older Xcode versions and CocoaPods accept iOS 13.0, macOS 10.15, tvOS 13.0, and watchOS 7.0.

Install the SDK

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

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

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

The package depends on AmplitudeSwift 1.17.0 or later, AmplitudeExperiment 1.19.0 or later, and, on iOS, AmplitudeSessionReplay 0.8.0 or later, all below 2.0.0. Your package manager resolves the newest versions in those ranges.

Fix installation problems

Initialize the SDK

Create one Amplitude instance when your app launches, and keep a strong reference to it, for example as a property of your App struct or AppDelegate. The Unified initializer creates Analytics, then adds the Session Replay plugin (iOS only) and the Feature Experiment plugin.

swift
import AmplitudeUnified

let amplitude = Amplitude(
    apiKey: "YOUR_API_KEY",
    serverZone: .EU, // Omit for US projects.
    sessionReplayConfig: SessionReplayPlugin.Config(sampleRate: 1.0) // Records every session. Lower this before production.
)

import AmplitudeUnified also makes AmplitudeSwift, Experiment, and Session Replay types available. You don't need to import them separately.

Session Replay records nothing until you set sampleRate above its default of 0. With the defaults, Amplitude(apiKey:) sets up Session Replay but never records.

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 in AnalyticsConfig. The SDK calls the callback once for each event after the upload finishes.

swift
let amplitude = Amplitude(
    apiKey: "YOUR_API_KEY",
    analyticsConfig: AnalyticsConfig(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

On a first launch, the callback prints a session_start line before Sign Up Clicked, because the SDK tracks sessions by default.

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

Fix upload problems

The Unified SDK uploads events through the bundled iOS Swift SDK, so the callback codes, messages, and upload logs match the iOS Swift SDK. For every code and log line, go to Fix upload problems in the iOS Swift SDK.

Confirm the event in Amplitude

  1. In Amplitude, open User Lookup and search for the device ID from amplitude.getDeviceId().
  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.

Next steps

이 내용이 도움이 되었나요?

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.

이 페이지에서