• SDKS
No results for ""


Xamarin SDK reference

Read time: 7 minutes
Last edited: Apr 02, 2021

This reference guide documents all of the methods available in our Xamarin 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 Xamarin SDK GitHub repository. Additionally you can clone and run sample applications using this SDK with Xamarin, and Xamarin.Forms. The online API docs contain the programmatic definitions of every type, property, and method.

For use in mobile, desktop, and embedded client applications only

This SDK is intended for use in single-user mobile, desktop, and embedded applications. If you have a .NET application and are looking to set up LaunchDarkly on the server-side, head to our .NET SDK reference.

To learn more, read client-side and server-side SDKs.

Getting started

Building on top of our Getting Started guide, the following steps will get you started with using the LaunchDarkly SDK in your Xamarin application.


The Xamarin SDK natively supports Android (version 7.1 or higher) or iOS (version 10 or higher); it can also be used on any other platform that supports .NET Standard (version 1.6 or higher), although the .NET Standard version lacks some mobile-specific features such as detecting network connectivity. Previous beta releases of the SDK used the Xamarin.Essentials library, but it no longer has the dependency.

1Install-Package LaunchDarkly.XamarinSdk

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

1using LaunchDarkly.Client;
2using LaunchDarkly.Xamarin;

Once the SDK is installed, you'll want to create a single, shared instance of LDClient.

Once the dependency is installed, you'll want to initialize the LaunchDarkly client. You'll want to create a single, shared instance of LdClient. To create a client instance, you need your environment's mobile key (available on your account settings page). Never embed a server-side SDK key into a mobile application.

The following example shows the simplest way to create the client. It will block for up to

  1. seconds until the latest feature flags have been retrieved from LaunchDarkly.
1var user = User.WithKey(user_key);
2var timeSpan = TimeSpan.FromSeconds(10);
3ldClient = LdClient.Init("MY_MOBILE_KEY", user, timeSpan);

However, calling blocking code from the main thread in an Android app is not considered a best practice. The preferred method (loading the client asynchronously) is shown below.

1User user = User.WithKey(user_key);
2ldClient = await LdClient.InitAsync("MY_MOBILE_KEY", user);
LdClient must be a singleton

It's important to make this a singleton. The client instance maintains internal state that allows us to serve feature flags without making any remote requests. Be sure that you're not instantiating a new client with every request.

Android requires AccessNetworkState permission

For Android, the AccessNetworkState permission is required and must be configured in the Android project. To learn more, read the Xamarin docs about how to implement this requirement.

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

1bool showFeature = ldClient.BoolVariation("your.feature.key", false);
2if (showFeature) {
3 // application code to show the feature
5else {
6 // the code to run if the feature is off

Customizing your client

You can also pass custom parameters to the client by creating a custom configuration object:

1Configuration config = LaunchDarkly.Client.Configuration
2 .Builder("YOUR_SDK_KEY")
3 .EventFlushInterval(TimeSpan.FromSeconds(2))
4 .Build();
5LdClient ldClient = LdClient.Init(config, user);

Here, we've customized the event queue flush frequency.

You can customize Configuration.Builder. To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs


Feature flag targeting and rollouts are all determined by the user you pass to your Identify call. In our Xamarin SDK, you can construct a simple User that only has a key by calling User.WithKey, or use User.Builder which allows setting all properties. Here's an example:

1LDUser user = User.Builder("aa0ceb")
2 .FirstName("Ernestina")
3 .LastName("Evans")
4 .Email("ernestina@example.com")
5 .Custom("group", "admins")
6 .Build();

Let's walk through this snippet. The argument to 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 (set by calls to FirstName, LastName, Email, and 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 the built-in attributes defined in the User class, 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 arbitrary JSON values. If you enter a custom value on our dashboard that looks like a number or a boolean, it'll be interpreted that way. The Xamarin 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, including organizations, groups, andaccount plans. Anything you pass to us becomes available instantly on our dashboard.

Private user attributes

You can optionally configure the Xamarin 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 Xamarin SDK there are two ways to define private attributes for the entire LaunchDarkly client:

  • When creating the LaunchDarkly Configuration object, you can call the AllAttributesPrivate method, which takes in a boolean parameter. If true, all user attributes (except the key) for all users are removed before the user is sent to LaunchDarkly.
  • When creating the LaunchDarkly Configuration object, you can call the PrivateAttributeName method, which takes in an attribute name (string) as a parameter and adds it to an internally managed list of private attributes. This method may be called multiple times to mark additional attributes as private. If any user has a custom or built-in attribute named in the private attributes 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 AsPrivateAttribute() immediately after setting the attribute. For example:

1var user = User.Builder("aa0ceb")
2 .Email("test@example.com").AsPrivateAttribute()
3 .Build();

When this user is sent back to LaunchDarkly, the email attribute will be omitted.

Anonymous users

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

1LDUser user = User.Builder("aa0ceb")
2 .Anonymous(true)
3 .Build();

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

We recommend using the same user key for every initialization and then replacing that with the actual user key when you know who the user is. This way LaunchDarkly counts the initialization user key only once against your MAU, instead of every time you initialize.

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 keeps anonymous users from polluting your Users page.

In Android or iOS, you can also allow the SDK to create a key for an anonymous user based on a unique device identifier as defined by the DeviceInfo Xamarin plugin. It will do so if you set the key to null and set the anonymous flag:

1LDUser user = User.Builder((string)null).Anonymous(true).Build();


The Variation method determines whether a flag is enabled or not for a specific user. In Xamarin, there is a variation method for each type (e.g. BoolVariation, StringVariation):

1ldClient.BoolVariation("your.feature.key", false);

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

The default 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 VariationDetail methods (BoolVariationDetail, etc.) work the same as Variation, but also provide additional "reason" information about how a flag value was calculated (for instance, if the user matched a specific rule). You can examine the "reason" data programmatically; you can also view it with Data Export, if you are capturing detailed analytics events for this flag. To enable this feature, you must call WithEvaluationReasons(true) on your Configuration.

To learn more, read Evaluation reasons.


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. Here's a simple example:


You can also attach custom data to your event by passing an extra parameter to Track (using the LdValue class to represent any value that can be encoded in JSON):

1ldClient.Track("Completed purchase", LdValue.Of("sku132"));

All Flags

Different behavior between calls

Unlike Variation and Identify calls, AllFlags does not send events to LaunchDarkly. Users from AllFlags calls are not created or updated in the LaunchDarkly dashboard.

The AllFlags method produces a map of feature flag keys to their values for the current user.


Fallback value and offline mode

The default (fallback) values are defined in your code. The default value will only be returned if an error is encountered or if LaunchDarkly is unreachable.

In some situations, you might want avoid remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on premise, it might make sense to fall back to defaults when running on premise. You can do this by setting offline mode in the client's Config. When the client is in offline mode, no network requests will be made, so it is suitable for unit-testing.

1Configuration config = Configuration.Builder("SDK_KEY")
2 .Offline(true)
3 .Build();
4LdClient client = LdClient.Init(config);


Internally, the LaunchDarkly SDK keeps an event buffer for Track and Identify 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.


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


Dispose safely shuts down the client instance and releases all resources associated with the client. In most long-running applications, you should not have to call dispose.


Using the Relay Proxy

You can configure the Xamarin SDK to connect to the Relay proxy as follows:

1Configuration config = Configuration.Builder("YOUR_SDK_KEY")
2 .StreamUri(new URI("YOUR_RELAY_URI"))
3 .Build();
4LdClient ldClient = LdClient.Init(config);


The Xamarin SDK uses the Common.Logging framework. For an example configuration check out the Common.Logging readme.

Be aware of two considerations when enabling the DEBUG log level:

  1. Debug-level logs can be very verbose. It is not recommended that you turn on debug logging in high-volume environments.
  2. Potentially sensitive information is logged including LaunchDarkly users created by you in your usage of this SDK.

Real-Time Updates

LaunchDarkly manages all flags for a user context in real-time by updating the flag cached 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. The default SDK configuration has been found to be the best combination of low latency updates and minimal battery drain:

  1. When the app is foregrounded, a Server-sent events streaming connection is made to LaunchDarkly. This streaming connection stays open as long as your app is in the foreground and is connected to the internet.

  2. When the app is backgrounded, the stream connection is terminated and the SDK will poll (with caching) for flag updates every 3 minutes.

  3. When the app is foregrounded, we reconnect to the stream and fetch the latest flags.

  4. In either the foreground or background, we don't try to update unless your device has internet connectivity.

This configuration means that you will get near real-time updates for your feature flag values when the app is in the foreground. See the "customizing your client" section above for information on how to change your configuration.

To perform real-time updates in your app, your app will need to register listeners for updates from the streaming/polling connection for each flag you'd like to watch:

1ldClient.FlagChanged += (sender, eventArgs) => {
2 if (eventArgs.Key == "key-for-flag-i-am-watching") {
3 DoSomethingWithNewFlagValue(eventArgs.NewBoolValue);
4 }

Data collection

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