Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
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.
Overview
This topic documents how to get started with the iOS SDK, and links to reference information on all of the supported features.LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:
| Resource | Location |
|---|---|
| SDK API documentation | SDK API docs |
| Supported SDK Versions | iOS SDK |
| GitHub repository | ios-client-sdk |
| Sample applications | iOS (Objective-C) , iOS (Swift) , macOS , tvOS |
| Published module | CocoaPods |
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
The first step is to install the LaunchDarkly SDK as a dependency in your application. We recommend making the LaunchDarkly observability plugin available as well. This plugin collects and sends observability data to LaunchDarkly. This means you can review error monitoring and logs from within the LaunchDarkly UI. It requires the iOS SDK version 9.14 or later, and is only available if you are using Swift. LaunchDarkly supports multiple methods for installing the SDK. Each method is explained below.Expand Use the Swift Package Manager
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 yourPackage.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
Expand Use CocoaPods
Use Cocoa
PodsIf you use CocoaPods, you can install the SDK by adding the following to yourPodfile. To identify the latest version, read the SDK releases page.Here is the code to add to your Podfile:Expand Use Carthage
Expand Use Carthage
Use Carthage
If you use Carthage, you can install the SDK by specifying it in yourCartfile. To identify the latest version, read the SDK releases page.Here is the code to include in your Cartfile:Expand Install the SDK manually
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. To do this, you need your environment’s mobile key and the context for which you want to evaluate flags. This authorizes your application to connect to a particular environment within LaunchDarkly. 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 atimedOut boolean to the completion closure, indicating whether the connection timed out.
Here’s how:
LDConfig.
Evaluate a flag
After callingstart, you can retrieve the LDClient instance with the static method LDClient.get():
client to check which variation a particular context will receive for a given feature flag.
Here’s how:
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 yourLDConfig:
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 callidentify, 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:- Anonymous contexts and users
- Automatic environment attributes
- Configuration, including
- Context configuration
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Hooks
- Identifying and changing contexts
- Logging configuration
- Monitoring SDK status
- Multiple environments
- Observability
- Offline mode
- Private attributes
- Relay Proxy configuration, using proxy mode
- Sending custom events
- Shutting down
- Subscribing to flag changes