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.
- After creating an application in Twitter it will generate a Twitter key and secret, which can be used to integrate our application with Twitter.
Implementation
- Create a new Xamarin Android application in Visual studio and add install the Xamarin.Digits Nuget package.
- Go to AndroidManifest.xml and add following in the application tag to integrate our app with fabric.
1<meta-data android:name="io.fabric.ApiKey" android:value="Api key here" />
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.
1 2 3 4 5 |
<com.digits.sdk.android.DigitsAuthButton android:id="@+id/auth_button" android:layout_gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> |
- Make use of the Twitter key and Twitter secret in our application by adding these values in string.xml in Resource? Values Folder
1 2 |
<string name="twitterKey">Twitter key here</string> <string name="twitterSecret"> Twitter secret here</string> |
- And in OnCreate() method add the following code to initialise the fabric application with Twitter into our application.
1 2 3 4 5 6 |
TwitterAuthConfig authConfig = new TwitterAuthConfig ( GetString(Resource.String.twitterKey), GetString(Resource.String.twitterSecret) ); Fabric.With(this, new TwitterCore(authConfig),new Digits.Builder().Build()); |
- Now we need to initialize the Digits button. Add the following code in MainActivity.cs.
1 2 |
var digitsAuthBtn= FindViewById<DigitsAuthButton>(Resource.Id.auth_button); digitsAuthBtn.Text = "Register with phone number"; |
- Extend the MainActivity with callback interface i.e., IAuthCallback, as shown below so that we can handle authentication failure and success scenarios.
1 2 3 4 5 6 7 8 9 10 11 |
public class MainActivity : Activity, IAuthCallback { public void Failure(DigitsException failureException) { //Authentication is failed. Do something! } public void Success(DigitsSession session, string phoneNumber) { //Authentication is success. Do something! } } |
- Now set the callback for Digits button.
1 |
digitsAuthBtn.SetCallback(this); |
- 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.
1 2 3 4 5 6 7 8 9 |
if(Digits.ActiveSession==null|| (Digits.ActiveSession.AuthTokenasTwitterAuthToken).IsExpired)) { //User is not logged in. Do something! } else { //User is logged in. Do something! } |
We can also get the user registered phone number at any time, with below line of code.
1 |
var phoneNumber = Digits.ActiveSession.PhoneNumber; |
And we can delete the user from the session (i.e. logout the user), with the below line of code.
1 |
Digits.SessionManager.ClearActiveSession(); |
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 repository. For more information about Digits go through the following link.
Google has acquired number of assets, including Digits from Twitter. Is still called Twitter Digits?
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.”