Xamarin SDK Reference

Updated 1 month ago by Archana Singh

This topic explains how to use the Harness Feature Flags (FF) SDK in your Xamarin application. Xamarin lets you build native apps for Android, iOS, and macOS using .NET code and platform-specific user interfaces. For more information on Xamarin, see Xamarin documentation.

To learn more about using a sample FF SDK code with the Xamarin application, see the Xamarin SDK GitHub repository.

In this topic:

Before You Begin

Prerequisites

  1. Create a feature flag in Harness Feature Flags. Feature flags wrap your code and allow you to manage the feature release in a controlled way. See Create a Feature Flag.
  2. Ensure that you have created your SDK Key. See Create an SDK Key.
  3. A Xamarin application to test your feature flag. If you do not have your Xamarin Application, you can download a sample application from the Xamarin SDK GitHub repository.
    1. On the Xamarin SDK GitHub page, click Code and then clone the sample application. For more information, see Cloning a repository.
    2. Import your project in an IDE such as IntelliJEclipse, or Xcode.

Use Harness Feature Flags SDKs with Xamarin Applications

Perform the following steps to get started with using the Feature Flags SDK in your Xamarin iOS and Android applications:

Step 1: Install the FF SDK Package

The first step is to install the FF SDK package in your application. 

iOS

Install-Package ff-ios-xamarin-client-sdk -Version 0.5.0

To be able to use the Android binding library the Google GSON bindings must be provided:


Install-Package GoogleGson -Version 2.8.5

Android

Install-Package ff-android-xamarin-client-sdk -Version 0.5.3

Step 2: Authorize your Application to Connect to the FF Client

After installing the SDK, enter the SDK keys that you created for your environment. The SDK keys authorize your application to connect to the FF client.

iOS

    // Create configuration:
var config = new CfConfigurationProxy
{
StreamEnabled = true,
AnalyticsEnabled = true
};

// Set selected identifer:
var target = new CfTargetProxy
{
Identifier = "target_identifier",
Name = "target_name"
};

// Initialize authentication. Update API_KEY with your key:
CfClientProxy.Shared.InitializeWithApiKey(API_KEY, config, target);

Android

       CfConfiguration configuration = new CfConfiguration.Builder()
.EnableStream(true)
.EnableAnalytics(true)
.Build();

Target target = new Target()
.InvokeName(account)
.InvokeIdentifier(account);

// Initialize authentication. Update API_KEY with your key.
// Listener object should implement IAuthCallback interface:
CfClient.Instance.Initialize(this.context, API_KEY, configuration, target, listener);

Step 3: Evaluate Target for Your Feature Flag

Once you have added the target, evaluate the target for your feature flag. A feature flag is evaluated for a particular target.

bool flag1 = CfClient.Instance.BoolVariation("flag1", false);

Available public variation methods are:

  • Boolean Variation
  • Number Variation
  • String Variation
  • JSON Variation

Step 4: Subscribe to Receive Library Events

iOS

using System;
using ff_ios_client_sdk_proxy;

public class CfListener : CfClientDelegate
{

public CfListener()
{
// Subscribe on getting events from native iOS library
CfClientProxy.Shared.Delegate = this;
}
// Received in case of error
public override void OnErrorWithError(CfErrorProxy error){}

// Contains array with flags received each pooling interval
public override void OnPollingEventReceivedWithEvaluations(CxEvaluation[] evaluations){}

// Event when flag value is changed
public override void OnStreamEventReceivedWithEvaluation(CxEvaluation evaluation){}

// Message received from library event
public override void OnMessageReceivedWithMessage(CxMessage message){}

// On Stream opened event
public override void OnStreamOpened() {}

// On Stream closed event
public override void OnStreamCompleted() {}
}

Android

using System;
using System.Linq;
using IO.Harness.Cfsdk.Cloud.Events;
using IO.Harness.Cfsdk.Cloud.Model;
using IO.Harness.Cfsdk.Cloud.Oksse;
using IO.Harness.Cfsdk.Cloud.Oksse.Model;
using Java.Interop;
using Java.Util;
using IO.Harness.Cfsdk;

public class CfListener : Java.Lang.Object, IAuthCallback, IEventsListener
{

public CfListener()
{
// Subscribe on getting events from native Android library
CfClient.Instance.RegisterEventsListener(this);
}
public void AuthorizationSuccess(AuthInfo p0, AuthResult p1)
{
// p1.Success contains status of authorization
// In case of error p1.Error contains error message
}
public void OnEventReceived(StatusEvent p0)
{
var eventType = p0.EventType;
if(StatusEvent.EVENT_TYPE.SseStart == eventType)
{
// Stream started
}
else if( StatusEvent.EVENT_TYPE.SseEnd == eventType)
{
// Stream Ended
}
else if (StatusEvent.EVENT_TYPE.EvaluationChange == eventType)
{
Java.Lang.Object payload = p0.ExtractPayload();
var ev = payload as IO.Harness.Cfsdk.Cloud.Core.Model.Evaluation;
// Flag changed event
}
else if (StatusEvent.EVENT_TYPE.EvaluationReload == eventType)
{
Java.Lang.Object payload = p0.ExtractPayload();

var t = payload.JavaCast<ArrayList>();
var arr = t.ToEnumerable<IO.Harness.Cfsdk.Cloud.Core.Model.Evaluation>().ToArray();

// Each pulling interval we will receive array of available flags.

}
}
}

Verify Flag Configuration Updates

Run the application from your IDE and verify whether the flag variation value displayed on your application page is consistent with the feature flag you created.

By toggling the flag on/off in Harness UI, the latest features are synced and the updated flag variation is served. Toggle the flag to verify if your application is getting updated.


Please Provide Feedback