• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL

EDIT ON GITHUB

iOS SDK reference

Read time: 2 minutes
Last edited: Jul 28, 2021
Swift and Objective-C compatibility

This documentation is for the latest version of the LaunchDarkly SDK for iOS.

SDK versions 4.0.0 and later are written in Swift. These versions are compatible with applications written in either Swift or Objective-C. See inline code samples throughout the documentation for use with either language.

This reference guide documents all of the methods available in the iOS SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source. To learn more, view the source on the iOS SDK GitHub repository or the generated API docs. Additionally you can clone and run sample applications using this SDK with iOS, macOS, and tvOS.

Getting started

Building on top of our Getting Started guide, the following steps will get you started with using the LaunchDarkly SDK in your application.

Including the SDK as a dependency

The first step is to install the LaunchDarkly SDK as a dependency in your application. LaunchDarkly supports multiple methods for installing the SDK in your application.

Swift Package Manager



If you use the Swift Package Manager, you can install the SDK through Xcode or include it as a dependency in your Package.swift file.

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter the iOS SDK repository URL clone URL, then select your desired version constraints.

Including the SDK as a dependency in a Package.swift file looks like this:

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

CocoaPods



If you use CocoaPods, you can install the SDK by adding the following to your Podfile. Refer to the SDK releases page to identify the latest version.

Here is the code to add to your Podfile:

1use_frameworks!
2target 'YourTargetName' do
3 pod 'LaunchDarkly', '~> 5.4'
4end

Carthage



If you use Carthage, you can install the SDK by specifying it in your Cartfile. Again, refer to the SDK releases page to identify the latest version.

Here is the code to include in your Cartfile:

1github "launchdarkly/ios-client" ~> 5.4

Installing the SDK manually



Refer to the SDK readme for instructions on installing the SDK without CocoaPods or Carthage.

Using the SDK in your application

This is a brief overview of using the SDK, see the detailed sections below for more information on configuration and advanced features.

Importing



First import the LaunchDarkly client in your application code:

1import LaunchDarkly

Initializing the SDK



Now that the SDK is imported, it can be configured and initialized. You should specify your mobile key when configuring the SDK so that your application will be authorized to connect to LaunchDarkly, retrieve flags for your environment, and report events.

Mobile Keys

Use a mobile key from your Environments page. Never embed a server-side SDK key into a mobile application.

Here is how to specify your mobile key:

1let config = LDConfig(mobileKey: "YOUR_MOBILE_KEY")
2let user = LDUser(key: "test-user")
3
4LDClient.start(config: config, user: user)
5
6// If you need to ensure that the most recent flags have been received
7// start supports an optional completion that is triggered when the SDK
8// has retrieved flags for the configured user.
9LDClient.start(config: config, user: user) {
10 // Client has received flags for the user
11}
12
13// If you would like the completion to be called even in cases where
14// the SDK is unable to retrieve flags for the user, there is a variant
15// to the start method that takes a maximum time to wait for flag values.
16// This variant notifies the completion whether the operation timed out.
17LDClient.start(config: config, user: user, startWaitSeconds: 5) { timedOut in
18 if timedOut {
19 // The SDK may not have the most recent flags for the configured user
20 } else {
21 // The SDK has received flags for the configured user
22 }
23}

Retrieving the client instance



After calling start, the LDClient instance can be retrieved with the static method LDClient.get():

1let ldClient = LDClient.get()!

Getting the variation to serve the configured user



Using the LDClient instance, you can check which variation the configured user should receive for a given feature flag:

1let showFeature = ldClient.variation(forKey: "flag-key", defaultValue: false)
2
3if showFeature {
4 // application code to show the feature
5else {
6 // the code to run if the feature is off
7}

Background fetch

When the app is backgrounded, the SDK does not receive real-time events. However, MacOS supports background fetch which updates flag values opportunistically, according to iOS standard defaults. iOS does not support background fetch.

To change the background fetch interval for flags in your app, add the following code in your LDConfig:

1var ldConfig = LDConfig(mobileKey: "MY_MOBILE_KEY")
2ldConfig.backgroundFlagPollingInterval = 3600

Lastly, down the client when your application terminates. To learn more, read Shutting down.

Data collection

To learn more about data collection within this SDK and implications on submissions to the Apple App Store, read Apple App Store data collection policy.

Supported features

This SDK supports the following features: