No results for ""
EXPAND ALL
  • Home
  • API docs

iOS SDK reference

Read time: 8 minutes
Last edited: Oct 02, 2024
Recent major versions

Version 9 of the iOS SDK introduces optional automatic collection of environment attributes. To learn more about upgrading, read iOS SDK 8.x to 9.0 migration guide.


Version 8 of the iOS SDK replaces users with contexts. A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. To learn more about upgrading, read the iOS SDK 7.x to 8.0 migration guides for Swift or Objective-C and Best practices for upgrading users to contexts.

Overview

This topic documents how to get started with the iOS SDK, and links to reference information on all of the supported features.

SDK quick links

LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:

ResourceLocation
SDK API documentationSDK API docs
GitHub repositoryios-client-sdk
Sample applicationsiOS (Objective-C)
iOS (Swift)
macOS
tvOS
Published moduleCocoaPods
SDK version compatibility

The LaunchDarkly iOS SDK, version 4.0.0 and higher, is compatible with applications written in either Swift or Objective-C. The inline code samples include both languages. The SDK is written in Swift.

Get started

After you complete the Get started process, follow these instructions to start using the LaunchDarkly SDK in your application:

  • Install the SDK
  • Import the SDK
  • Initialize the client
  • Evaluate a flag

Install the SDK

The first step is to install the LaunchDarkly SDK as a dependency in your application.

LaunchDarkly supports multiple methods for installing the SDK. Each method is explained below.

Expand Use the Swift Package Manager

Use the 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:

Expand Use CocoaPods

Use CocoaPods



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

Here is the code to add to your Podfile:

Expand Use Carthage

Use Carthage



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

Here is the code to include in your Cartfile:

Expand Install the SDK manually

Install the SDK manually



For instructions on installing the SDK without CocoaPods or Carthage, read the SDK readme.

Import the SDK

After you install the SDK as a dependency, import the LaunchDarkly client in your application code:

Initialize the client

After importing the SDK, configure and initialize the client. Specify your mobile key when configuring the SDK so that your application is authorized to connect to a particular environment within LaunchDarkly.

The iOS SDK uses a mobile key

The iOS SDK uses a mobile key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.


Mobile keys are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.

The following example shows how to configure the SDK, specify your mobile key, and initialize the client.

We recommend calling the client initialization method with a timeout of five seconds or fewer. The SDK provides a timedOut boolean to the completion closure, indicating whether the connection timed out.

Here's how:

To learn more about the specific configuration options available in this SDK, read LDConfig.

LDClient must be a singleton

It's important to make LDClient a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.

If you have multiple LaunchDarkly projects, you should use the multiple environments feature. To learn more, read Multiple environments.

Evaluate a flag

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

Then, use the client to check which variation a particular context will receive for a given feature flag.

Here's how:

Making feature flags available to this SDK

You must make feature flags available to mobile SDKs before the SDK can evaluate those flags. If an SDK tries to evaluate a feature flag that is not available, the context will receive the fallback value for that flag.

To make a flag available to this SDK, check the SDKs using Mobile key checkbox during flag creation, or on the flag's settings page. To make all of a project's flags available to this SDK by default, check the SDKs using Mobile key checkbox on your project's Flag settings page.

Background fetch

When the app is backgrounded, the SDK does not receive real-time events.

Unlike other mobile SDKs, the iOS SDK does not support background fetch, so devices on the iOS operating system will not fetch flags from the background. However, devices on MacOS will update flag values opportunistically, according to the iOS SDK standard background polling defaults.

To change the background polling default for flags in your app, add the following code in your LDConfig:

Shut down the client

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

Data collection

The data collected by the iOS SDK persists until the number of cached contexts exceeds a limit. When you call identify, the number of cached contexts increments. Eventually, the number of cached contexts exceeds maxCachedContexts. When that happens, the SDK deletes context data in excess of maxCachedContext, starting with the oldest context first.

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

Supported features

This SDK supports the following features: