• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL
launchdarkly.com

EDIT ON GITHUB

Xamarin SDK reference

Read time: 2 minutes
Last edited: Jun 18, 2021

This reference guide documents all of the methods available in the 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.

Requirements

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
4}
5else {
6 // the code to run if the feature is off
7}

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);

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.

Supported features

This SDK supports the following features: