Set up iOS SDK

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