The Customer Data Platform for Developers
Website · Documentation · Community Slack
The Swift SDK enables you to track customer event data from your iOS, macOS, tvOS, and watchOS applications and send it to your configured destinations via RudderStack.
- Installing the Swift SDK
- Initializing the SDK
- Identifying users
- Tracking user actions
- Contact us
- Follow Us
Add the SDK to your Swift project using Swift Package Manager:
- In Xcode, go to
File > Add Package Dependencies
- Enter the package repository URL:
https://github.com/rudderlabs/rudder-sdk-swift
in the search bar. - Select the version you want to use
- Select the project to which you want to add the package.
- Finally, click on Add Package.
Alternatively, add it to your Package.swift
file:
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "RudderStack",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "RudderStack",
targets: ["RudderStack"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/rudderlabs/rudder-sdk-swift.git", from: "<latest_version>")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "RudderStack",
dependencies: [
.product(name: "RudderStackAnalytics", package: "rudder-sdk-swift")
]),
.testTarget(
name: "RudderStackTests",
dependencies: ["RudderStack"]),
]
)
The SDK supports the following platforms:
- iOS 15.0+
- macOS 12.0+
- tvOS 15.0+
- watchOS 8.0+
To initialize the RudderStack Swift SDK, add the Analytics initialization snippet to your application's entry point:
import RudderStackAnalytics
class AppDelegate: UIResponder, UIApplicationDelegate {
var analytics: Analytics?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Initialize the RudderStack Analytics SDK
let config = Configuration(
writeKey: "<WRITE_KEY>",
dataPlaneUrl: "<DATA_PLANE_URL>"
)
self.analytics = Analytics(configuration: config)
return true
}
}
Replace:
<WRITE_KEY>
: Your project's write key from the RudderStack dashboard.<DATA_PLANE_URL>
: The URL of your RudderStack data plane.
The identify
API lets you recognize a user and associate them with their traits:
analytics?.identify(
userId: "1hKOmRA4el9Zt1WSfVJIVo4GRlm",
traits: [
"name": "Alex Keener",
"email": "[email protected]"
]
)
The track
API lets you capture user events:
analytics?.track(
name: "Order Completed",
properties: [
"revenue": 30.0,
"currency": "USD"
]
)
For more information:
- Email us at [email protected]
- Join our Community Slack