• SDKS
No results for ""


Legacy iOS SDK reference

Read time: 7 minutes
Last edited: Nov 05, 2020
iOS SDK versions 3.x and earlier

This documentation is for the LaunchDarkly SDK for iOS versions 3.0.1 and earlier. Versions 3.x and earlier are written in Objective-C. Versions 4.0.0 and later are written in Swift.

Per our End of Life policy, versions of the Objective-C based SDK are no longer supported. This reference guide remains available for customers who have yet to upgrade to the Swift-based SDK.

This reference guide documents all of the methods available in our 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, read iOS SDK GitHub repository. 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 help you use the LaunchDarkly SDK in your iOS application.

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.

If you're using CocoaPods, you can install the SDK by adding the following to your Podfile. Refer to the SDK releases page to identify the latest version. Note that versions starting with 4.0.0 support Swift instead of Objective-C.

1target 'TargetName' do
2 platform :ios, '8.0'
3 pod 'LaunchDarkly', '3.0.4'

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

1github "launchdarkly/ios-client" "3.0.4"

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

Next you should import the LaunchDarkly client in your application code.

1#import "Darkly.h"

Once the SDK is installed and imported, you'll want to create a single, shared instance of LDClient. You should specify your mobile key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

1LDConfig *config = [[LDConfig alloc] initWithMobileKey:@"YOUR_MOBILE_KEY"];
3LDUserBuilder *user = [[LDUserBuilder alloc] init];
4user.key = @"aa0ceb";
6[[LDClient sharedInstance] start:config withUserBuilder:user];
Mobile keys

Be sure to use a mobile key from your Environments page. Never embed a server-side SDK key into a mobile application.

Using LDClient, you can check which variation a particular user should receive for a given feature flag.

1BOOL showFeature = [[LDClient sharedInstance] boolVariation:@"YOUR_FLAG_KEY" fallback:NO];
2if (showFeature) {
3 NSLog(@"Showing feature for %@", user.key);
4} else {
5 NSLog(@"Not showing feature for user %@", user.key);

Lastly, when your application is about to terminate, shut down LDClient. This ensures that the client releases any resources it is using, and that any pending analytics events are delivered to LaunchDarkly. If your application quits without this shutdown step, you may not see your requests and users on the dashboard, because they are derived from analytics events. This is something you only need to do once.

1// In iOS, you would shut down the client like this:
2[[LDClient sharedInstance] stopClient];

Customizing your client

You can also pass other custom parameters to the client in the configuration object:

1config.connectionTimeout = [NSNumber numberWithInt:10];
2config.flushInterval = [NSNumber numberWithInt:10];
4[[LDClient sharedInstance] start:config withUserBuilder:user];

Here, we've customized the client connect and flush interval parameters.

Network access

By default, the LaunchDarkly client will need network access to *.launchdarkly.com using https.


Feature flag targeting and rollouts are all determined by the user you pass to your client. In our iOS SDK, we use a builder pattern to make it easy to construct users. Here's an example:

1LDUserBuilder *user = [[LDUserBuilder alloc] init];
3user.key = @"aa0ceb";
4user.firstName = @"Ernestina";
5user.lastName = @"Evans";
6user.email = @"ernestina@example.com";
7[user customArray:@"groups" value:groups];

Let's walk through this snippet. The first argument to the builder is the user's key. In this case we've used the hash "aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an e-mail address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.

All of the other attributes (like firstName, email, and the custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.

In addition to built-in attributes like names and e-mail addresses, you can pass us any of your own user data by passing custom attributes, like the groups attribute in the example above.

A note on types

Most of our built-in attributes (like names and e-mail addresses) expect string values. Custom attributes values can be strings, booleans (like true or false), numbers, or lists of strings, booleans or numbers.

If you enter a custom value on our dashboard that looks like a number or a boolean, it'll be interpreted that way. The iOS SDK is strongly typed, so keep this distinction in mind.

Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us, including organizations, groups, andaccount plans. Anything you pass to us becomes available instantly on our dashboard.

Private user attributes

You can optionally configure the iOS SDK to treat some or all user attributes as private user attributes. Private user attributes can be used for targeting purposes, but are removed from the user data sent back to LaunchDarkly.

In the iOS SDK there are two ways to define private attributes for the entire LaunchDarkly client:

  • When creating the LDConfig object, you can set the allUserAttributesPrivate attribute to YES
  • When creating the LDConfig object, you can set the privateUserAttributes attribute to a list of user attribute names, such as @[@"name", @"email"];. If any user has a custom or built-in attribute named in this list, it will be removed before the user is sent to LaunchDarkly.

You can also mark attributes as private when building the user object itself by calling the equivalent “private” user builder method. For example:

1LDUserBuilder *user = [[LDUserBuilder alloc] init];
3user.key = @"aa0ceb";
4user.email = @"ernestina@example.com";
5user.privateAttributes = @[@"email"];

Anonymous users

You can also distinguish logged-in users from anonymous users in the SDK, as follows:

1LDUserBuilder *user = [[LDUserBuilder alloc] init];
3user.key = @"aa0ceb";
4user.isAnonymous = TRUE;

You will still need to generate a unique key for anonymous users. Session IDs or UUIDs work best for this.

Anonymous users work just like regular users, except that they won't appear on your Users page in LaunchDarkly. You also can't search for anonymous users on your Features page, and you can't search or autocomplete by anonymous user keys. This is actually a good thing, because it keeps anonymous users from polluting your Users page!


The variation method determines whether a flag is enabled or not for a specific user. In iOS, there is a variation method for each type (e.g. boolVariation, stringVariation):

1[[LDClient sharedInstance] boolVariation:@"your.feature.key" fallback:FALSE];

variation calls take the feature flag key and a fallback value.

The fallback value will only be returned if an error is encountered. For example, the default value returns if the feature flag key doesn't exist or the user doesn't have a key specified.

The variation call will automatically create a user in LaunchDarkly if a user with that user key doesn't exist already. There's no need to create users ahead of time (but if you do need to, take a look at Identify).

Handling flag values on initial app launch

When LDClient is initialized for the first time at app launch, users will receive the feature flag fallback values until polling is completed for the first time.

You can use the userDidUpdate delegate method to be notified when the feature flag values have been polled and are ready to use. To learn more, read Realtime UI Updates. Once the flags have been polled for the first time, the SDK will always use the latest stored flag values thereafter.


The track method allows you to record actions your users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests. You can also attach custom JSON data to your event by passing an extra NSDictionary parameter to track. Here's a simple example:

1[[LDClient sharedInstance] track:@"your-goal-key" data:dict];

Offline mode

In some situations, you might want to stop making remote calls to LaunchDarkly and switch to fallback values for your feature flags. offline lets you do this easily.

1[[LDClient sharedInstance] offline];
2[[LDClient sharedInstance] boolVariation:@"your.feature.key" fallback:FALSE]; // will always return the fallback value (FALSE)

You can bring LaunchDarkly back online by calling online.

Airplane/Flight Mode

If a user's device is in airplane/flight mode or if they are not connected to a network, LaunchDarkly will use the latest stored flag settings in CoreData. If there are no previously stored flag settings, then the fallback values will be used.


Internally, the LaunchDarkly SDK keeps an event buffer for track calls. These are flushed periodically in a background thread. In some situations (for example, if you're testing out the SDK in a REPL), you may want to manually call flush to process events immediately.

1[[LDClient sharedInstance] flush];

The flush interval is configurable. If you need to change the interval, you can do so via the configuration.

Real-time updates

LaunchDarkly manages all flags for a user context in real-time by updating flags based on a real-time event stream. When a flag is modified from the LaunchDarkly dashboard, the flag values for the current user will update almost immediately.

To accomplish real-time updates, LaunchDarkly broadcasts an event stream that is listened to by the iOS SDK. Whenever an event is performed on the dashboard, the iOS SDK is notified of the updated flag settings in real-time.

To perform real-time updates in your app, your base class will need to conform to the ClientDelegate protocol. When your flag configurations change, either the userDidUpdate or the userUnchanged method of the protocol is called. Additionally, the featureFlagDidUpdate method of the protocol is called (with the specific flag key) when a flag value is changed.

If more than one class relies on LaunchDarkly’s feature flags, we recommend you post a notification in this method, in a manner similar to the following:

1- (void)userDidUpdate{
2 [[NSNotificationCenter defaultCenter] postNotificationName:@"userUpdatedNotification" object:nil];

After this, you can add a notification observer to any class that needs to update the app based on the flag values. This observer will point to a selector which can then update your app accordingly.

Variation methods

Make sure to use variation methods to get new flag values instead of any locally stored variables.

Background fetch

When the app is backgrounded, the iOS SDK does not receive real-time events. However, there is support for a background fetch to update flag values opportunistically, according to iOS standard defaults.

To allow background fetch for flags in your app, just add the following code in your AppDelegate:

1-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
2 [[NSNotificationCenter defaultCenter] postNotificationName:kLDBackgroundFetchInitiated object:nil];

You will also need to enable Background fetch in your Target’s capabilities.

Changing the user context

If your app is used by multiple users on a single device, then you will want to ensure that each user has persistent and personalized flag settings. To achieve this, the SDK will store up to 5 user contexts on a single device, with support for switching between different user contexts.

You can use the updateUser method to switch user contexts:

1LDUserBuilder *user = [[LDUserBuilder alloc] init];
2user.key = @"aa0ceb";
3[[LDClient sharedInstance] updateUser:user];

Multiple environments

LaunchDarkly's iOS SDK supports having multiple LDClient instances tied to separate mobile keys. This allows evaluating flags from multiple environments.

All LDClient instances will evaluate against the same LDUser. The mobile keys for additional environments are specified, along with identifying names, in a dictionary passed to your LDConfig object.

1LDConfig *config = [[LDConfig alloc] initWithMobileKey:@"YOUR_MOBILE_KEY"];
2NSDictionary *otherMobileKeys = [[NSDictionary alloc] initWithObjectsAndKeys:@"PLATFORM_MOBILE_KEY", @"platform", nil];
3[config setSecondaryMobileKeys:otherMobileKeys];
5LDUserBuilder *user = [[LDUserBuilder alloc] init];
6user.key = @\"aa0ceb\";
8[[LDClient sharedInstance] start:config withUserBuilder:user];

To access the secondary mobile key instances, use the environmentForMobileKeyNamed method on LDClient. This method takes the identifier name assigned to your environment key in the secondaryMobileKeys dictionary and returns the associated LDClient instance.

1LDClient *coreInstance = [[LDClient sharedInstance] environmentForMobileKeyNamed:@"platform"];
2[coreInstance boolVariation:@"platform-bool-flag" fallback:FALSE];

As all the client instances use the same LDUser object, some calls will affect all instances. These methods include: updateUser, flush, stopClient, and set online/offline. Track calls, listeners, and flag evaluation are all tied to the client instance they are evaluated against.