Digits helps you onboard your mobile app users with simple phone number authentication so you can build safe, viral communities. Don’t ask people to create passwords they’ll forget – delight them with a quick and easy login experience.
Digits bindings for xamarin
Digits is built using Java & Objective-C for android & iOS platforms which means its not ready available for xamarin developers yet, but we can still make use of this native component by creating a binding library. No am not gonna talk about how to create a binding library for Android & iOS, instead am going to create a sample making use of the digits binding library the one which i’ve created recently (you can find that from the repo here).
Digits-xamarin sample:
Before we start with Fabric Digits, the common procedure which we need to follow in order to develop an application is explained below.
- In general we need api key to work with fabric Digits. Which will be generated by creating a Fabric Account here. We should need Android studio for getting API Keys. So make sure we have Android Studio Installed (just for generating API keys).
- Create a new application in android studio and add a plug-in to our application which is clearly explained with images and we see them after creating an account in Fabric here.
- After adding plug-in to the application in android studio, Login to Fabric site with our account and select Digits from the kits listed, and follow the procedure explained in corresponding Digits kit.
- Create Digits Account, and get the API keys obtained from AndroidManifest.xml file and MainActivity.java.
- 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.
- And now we have generated a fabric api key. So, Android studio in not required to proceed further.
- 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 sample android application in Visual studio and install the Digits Nuget package.
Procedure to add nuget package
Follow the below steps to add to nuget package to our application in Visual Studio.
- Go to solution explorer window, there we can find References right click on it and select Manage NuGet Packages.
- And search for the package Digits, and select the package and click on install.
- And we can see the “Finished” message on the output window after the successful installation of the package.
After successfull installation of Fabric packages into our application follow the below steps.
- Go to AndroidManifest.xml and add following in the application tag, to integrate our app with fabric.
<meta-data android:name=“io.fabric.ApiKey” android:value=“Api key here” />
- We have an existing digits button to initiate the authentication process. So, make use of that button in our application by adding below code in Main.axml
<com.digits.sdk.android.DigitsAuthButton android:id="@+id/auth_button" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="wrap_content" />
- Make use of Twitter Key and Twitter secret in our application by adding these values in string.xml in Resource→ Values Folder
Twitter key here Twitter secret here
- And in onCreate() method add the following code to initialise the fabric application with Twitter into our application.
TwitterAuthConfig authConfig = new TwitterAuthConfig( GetString(Resource.String.twitterKey), GetString(Resource.String.twitterSecret));
Fabric.With(this, new TwitterCore(authConfig), new Digits.Builder().Build());
- Extend the MainActivity with Callback interface as shown below.
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.
digitsAuthBtn.SetCallback(this);
- Now we can build and run the application and after successful authentication with phone number the Success() method will be called.
- The output is shown below.
- And for more understanding about Digits just go through with link here.
Session Management
By default, session is managed by digits i.e. once the user is logged in using the phone number and again user reopen the application no need to log in again, he can use the application with the previously registered phone number. But the only thing we could do is checking the registered user info before the start of an application.
The below line of code used to check whether the user is already logged in or not.
if (Digits.ActiveSession == null ||(Digits.ActiveSession.AuthToken as TwitterAuthToken).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.
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.
Digits.SessionManager.ClearActiveSession();
In the following screen, we can see how we actually displays the screen after user is logged in.
Contact Integration
Refer to Fabric Digits sight to upload users address book to find other Digits users in their app.
Summary
In this article , we have understood the process of intergrating digits framework into Xamarin.Android application to facilitate users logging in by entering the phone number. We also discussed saving the user data securely inside the app store (cache), so that user does not need to give the details every time they open the app.