Scroll Top

Integrating Twitter Digits with Xamarin Android bindings

Overview

User registration is a key component for most mobile applications. It is perhaps also the most cumbersome from an end user perspective. Done wrong you risk turning away users right at the gate. Traditionally, applications use the custom username & password schemes or perhaps integrate with social media accounts. There exists a third option, in the form of Digits, a component of the Twitter Fabric SDK.

Digits use SMS-based authentication. Instead of a login screen, the user is prompted for a phone number. Twitter Fabric then generates a one time pin and sends it back to the user via SMS.

Walking Tree supports Xamarin bindings for the Twitter Fabric SDK. These bindings are available as Nuget packages. In this blog, we will be explaining how to make us of Fabric Digits in Xamarin Android.

Getting Started

Here are the steps to get started:

  • To begin with, create a Twitter Fabric account (here) and register for an API Key.
  • Create a new application in android studio and add a plug-in to our application. Twitter Fabric has a nice tutorial of this process here.
  • After adding the plug-in to the application in Android studio, login to Fabric site with your account and select Digits from the kits listed, and follow the procedure explained in corresponding  Digits kit.
  • Create Digits Account, and make a note of API key value which is present in AndroidManifest.xml file as shown below.
  • Build the project in Android Studio. Once the application is built from Android Studio, our application gets listed in Fabric dashboard. And get all the required  API keys.
  • With the API key generated, the Android Studio is no longer required.
  • And now we have to integrate our application with Twitter which can be done by creating an application in Twitter Developer Console.

Implementation

  • Go to AndroidManifest.xml and add following in the application tag to integrate our app with fabric. 

NOTE: Get the key from the figure (a) and replace it in the place of “API key here.”

  • Add below the line of code to LinearLayout inside the Main.axml to implement the authentication for Digits.

  • Make use of the Twitter key and Twitter secret in our application by adding these values in string.xml in Resource? Values Folder

  • And in OnCreate() method add the following code to initialise the fabric application with Twitter into our application.

  • Now we need to initialize the Digits button. Add the following code in MainActivity.cs.

  • Extend the MainActivity with callback interface i.e., IAuthCallback, as shown below so that we can handle authentication failure and success scenarios.

  • Now set the callback for Digits button.

  • Now we can build and run the application and after successful authentication, with the phone number, the Success() method will be called.

Now that you’ve understood the process of integrating Digits in your Xamarin.Android application, I would like to introduce you to a couple of important features which enables you to avoid login again and again and find other Digits users in your app.

Session Management

Caching is a technique of storing frequently used data/information in memory, so that, when the same data/information is needed next time, it could be directly retrieved from the memory. Digits provide caching facility by default.

The below line of code used to check whether the user is already logged in or not.

We can also get the user registered phone number at any time, with below line of code.

And we can delete the user from the session (i.e. logout the user), with the below line of code.

In the following screen, we can see how we actually display the screen after the user is logged in.

Contact Integration

Refer to Fabric Digits sight to upload the users’ address book to find other Digits users in their app.

Summary

In this article, we have understood the process of integrating the digits framework into Xamarin.Android application to facilitate users logging in by entering the phone number. We also discussed how to retrieve a digit’s session using the handler (cache), so that user does not need to give the details every time they open the app.

You can find the sample application in this repositoryFor more information about Digits go through the following link.

Comments (2)

Google has acquired number of assets, including Digits from Twitter. Is still called Twitter Digits?

Venkataramana Madugula

Thanks for the question Alok. Yes agreed that fabric is joining Google.
Here is one of the reasons for mentioning twitter digits in the article – source : <>
“During a transition period, Digits will continue to be maintained by Twitter under its existing terms. If you have any questions or need support for Digits, please ping the Twitter Community forums as usual.”

Leave a comment

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.