• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""
EXPAND ALL

GIVE DOCS FEEDBACK

iOS SDK reference

Read time: 9 minutes
Last edited: Jan 29, 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. Contexts replace another data object in LaunchDarkly: "users." 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.


Code samples on this page are from the three most recent SDK versions where they differ.

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.

Getting started

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

  • Including the SDK as a dependency
  • Importing the SDK
  • Initializing the SDK

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. Each method is explained below.

Expand Using the Swift Package Manager

Using 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:

//...
dependencies: [
.package(url: "https://github.com/launchdarkly/ios-client-sdk.git", .upToNextMinor("9.0.0")),
],
targets: [
.target(
name: "YOUR_TARGET",
dependencies: ["LaunchDarkly"]
)
],
//...
Expand Using CocoaPods

Using 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:

use_frameworks!
target 'YourTargetName' do
pod 'LaunchDarkly', '~> 9.0'
end
Expand Using Carthage

Using 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:

github "launchdarkly/ios-client" ~> 9.0
Expand Installing the SDK manually

Installing the SDK manually



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

Importing the SDK

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

import LaunchDarkly

Initializing the SDK

After importing the SDK, configure and initialize it. 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. Your environment's mobile key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.

Never embed a server-side SDK key into a client-side application

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. The start() call to initialize the client executes asynchronously, to avoid blocking the main thread. If you need to ensure that the most recent flags have been received, start() supports an optional completion that is triggered when the SDK has retrieved flags for the configured context.

Here's how:

let config = LDConfig(mobileKey: "mobile-key-123abc", autoEnvAttributes: .enabled)
let context = LDContextBuilder(key: "context-key-123abc")
LDClient.start(config: config, context: context) {
// Client has received flags for the context
}

You can also choose to execute code either as soon as flags have been retrieved, or after a set amount of time, whichever comes first. This sets a maximum waiting period for establishing the connection. The following example uses five seconds. The SDK provides a timedOut boolean for the completion closure, indicating whether the connection timed out.

Here's how:

let config = LDConfig(mobileKey: "mobile-key-123abc", autoEnvAttributes: .enabled)
let context = LDContextBuilder(key: "context-key-123abc")
LDClient.start(config: config, context: context, startWaitSeconds: 5) { timedOut in
if timedOut {
// Client may not have the most recent flags for the configured context
} else {
// Client has received flags for the configured context
}
}
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.

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

let client = LDClient.get()!

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

Here's how:

let showFeature = client.boolVariation(forKey: "flag-key-123abc", defaultValue: false)
if showFeature {
// Application code to show the feature
else {
// The code to run if the feature is off
}
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 tab. To make all of a project's flags available to this SDK by default, check the SDKs using Mobile key checkbox in your project Settings.

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:

var ldConfig = LDConfig(mobileKey: "mobile-key-123abc", autoEnvAttributes: .enabled)
ldConfig.backgroundFlagPollingInterval = 3600

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: