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-- head to our iOS SDK GitHub repository to look under the hood.
If you haven't taken a look at our Quickstart guide yet, we recommend starting there to see how install our SDK into your iOS application.
Once the SDK is installed, you'll want to create a single, shared instance of
LDClient, passing in the client configuration object and a user:
LDConfigBuilder *config = [[LDConfigBuilder alloc] init]; [config withApiKey:@"YOUR_MOBILE_KEY"]; LDUserBuilder *user = [[LDUserBuilder alloc] init]; user = [user withKey:@"aa0ceb"]; [[LDClient sharedInstance] start:config userBuilder:user];
Be sure to use a mobile key from your Environments page. Never embed a standard API key into a mobile application.
You can also pass other custom parameters to the client via the configuration object:
[config withConnectionTimeout:10]; [config withFlushInterval:10]; ... [[LDClient sharedInstance] start:config userBuilder:user];
Here, we've customized the client connect and flush interval parameters.
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:
LDUserBuilder *user = [[LDUserBuilder alloc] init]; user = [user withKey:"aa0ceb"]; user = [user withFirstName:"Ernestina"]; user = [user withLastName:"Evans"]; user = [user withEmail:"email@example.com"]; user = [user withCustomArray:"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
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 be aware of this distinction.
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-- organizations, groups, account plans-- anything you pass to us becomes available instantly on our dashboard.
You can also distinguish logged-in users from anonymous users in the SDK, as follows:
LDUserBuilder *user = [[LDUserBuilder alloc] init]; user = [user withKey:"aa0ceb"]; user = [user withAnonymous: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-- it keeps anonymous users from polluting your Users page!
toggle method determines whether a flag is enabled or not for a specific user.
[[LDClient sharedInstance] toggle:@"your.feature.key" default:FALSE];
toggle calls take the feature flag key and a default value.
toggle works by running through the targeting rules first. If a user is specifically included or excluded according to the targeting rules,
toggle will return
FALSE accordingly. If none of the targeting rules match,
toggle will determine whether the user falls into the percent rollout.
The default value will only be returned if the kill switch has been triggered or an error is encountered-- for example, if the feature flag key doesn't exist or the user doesn't have a key specified.
toggle 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.
When you make changes on your dashboard, you might notice that it takes at least 1 minute for those changes to be reflected in your mobile app.
Our iOS SDK respects the TTL specified on your Account Settings page. However, the SDK enforces a minimum TTL of 1 minute.
This means that changes on your dashboard will take effect in
TTL minutes, or in 1 minute if your TTL is set to 0.
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. Here's a simple example:
[[LDClient sharedInstance] track:@"Signed up"];
You can also attach custom JSON data to your event by passing an extra
NSDictionary parameter to
[[LDClient sharedInstance] track:@"Signed up" data:dict];
In some situations, you might want to stop making remote calls to LaunchDarkly and fall back to default values for your feature flags.
offline lets you do this easily.
[[LDClient sharedInstance] offline:TRUE]; [[LDClient sharedInstance] toggle:@"your.feature.key" default:FALSE]; // will always return the default value (FALSE)
You can bring LaunchDarkly back online by calling
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.
[[LDClient sharedInstance] flush];
Note that the flush interval is configurable-- if you need to change the interval, you can do so via the configuration.