Set up iOS SDK

LaunchDarkly onboarding is not available in federal environments
To learn more, read LaunchDarkly in federal environments.

Install the package

1//...
2 dependencies: [
3 .package(url: "https://github.com/launchdarkly/ios-client-sdk.git", .upToNextMajor("9.15.0")),
4 ],
5 targets: [
6 .target(
7 name: "YOUR_TARGET",
8 dependencies: ["LaunchDarkly"]
9 )
10 ],
11//...

Initialize the SDK

iOS SDK initialization
1import LaunchDarkly
2
3// This is your mobile key.
4let config = LDConfig(mobileKey: "YOUR_MOBILE_KEY", autoEnvAttributes: .enabled)
5
6// A "context" is a data object representing users, devices, organizations, and other entities.
7let contextBuilder = LDContextBuilder(key: "EXAMPLE_CONTEXT_KEY")
8guard case .success(let context) = contextBuilder.build()
9else { return }
10
11LDClient.start(config: config, context: context, startWaitSeconds: 5) { timedOut in
12 if timedOut {
13 print("SDK didn't initialize in 5 seconds. SDK is still running and trying to get latest flags.")
14 } else {
15 print("SDK successfully initialized with the latest flags")
16 }
17}
18
19print("SDK started.")

You can find your server-side SDK key, client-side ID, and mobile key in the “Resources” section of the help menu. Click the help icon at the bottom left corner of the LaunchDarkly UI, then choose SDK keys:

The SDK keys option in the help menu.

The SDK keys option in the help menu.

To learn more, read Initialize the client in the iOS SDK reference guide.