Using Native Facebook Login Button in Xamarin.Forms QA

On 9th of March 2018 I wrote an article “Using Native Facebook Login Button in Xamarin.Forms” which turned out to be the second popular article on my blog. Since then, thanks for my readers, I received many additional questions which I decided to answer in this separate article. However, before diving into those questions, I would like to take a step back and talk about Facebook SDK in relation to Xamarin.

Facebook SDK is available for Xamarin.Android and Xamarin.iOS but not for Xamarin.Forms. This might be the most confusing part, especially if you are working with Facebook SDK on Xamarin.Forms for the first time. Luckily, the problem could be easily solved, please follow the original article.

Now the question is “Do you actually need to integrate the official Facebook Login component to your app?”. What if you want a fully customised button instead of the official look and feel? What if you just want to obtain an authentication token from Facebook and pass it to your backend? By the end of the day we just want to be productive and write as less code as possible, right? If I just described your case please refer to my other article where this can be achieved with a couple lines of code with a ready made wrapper for Xamarin.Forms. Continue reading “Using Native Facebook Login Button in Xamarin.Forms QA”

Retrieving Facebook User Access Token in Xamarin.Forms

Few months ago I wrote an article about Using Native Facebook Login Button in Xamarin.Forms where I explained how to retrieve user access token using Facebook SDK. It is still a valid read and a good solution, however, recently I discovered that there is a shorter way to achieve almost the same thing.

In this article we will learn how to retrieve Facebook user access token, using a custom button (not native) with just a few lines of code. I assume that you know how to create a Facebook application and configure your Android and iOS projects. If I am mistaken please refer to my previous article.

There is an open source project Facebook Client Plugin that allow you to login, share and query Facebook using static API. It has a good documentation and good well known developers behind it. As you may already understand we are going to use it to retrieve user access token from Facebook in our project.

Here is the recipe:

  1. Create and configure Facebook app (more info here or here).
  2. Add Plugin.FacebookClient NuGet package to .NET Standard and platform targeting projects.
  3. Configure platform targeting projects (more info here or here).
  4. Add a custom Facebook login button to your Page/View.
  5. Bind a command to the previously created button with the next code:
    var fbLoginResponse=
    if(fbLoginResponse.Status == FacebookActionStatus.Completed)
        var fbUserAccessToken=CrossFacebookClient.Current.ActiveToken;
        // TODO: Use the fb access token
        // Something went wrong

That should be it!

You could create a wrapper interface and class to make this code testable, to be able to inject it as a dependency and to adjust it to your needs. This solution might be less configurable, however, the integration is very easy and the amount of code is miserable compare to the previous solution.

Big thanks to CrossGeeks!

Using Native Facebook Login Button in Xamarin.Forms

This slideshow requires JavaScript.

Very often I hear questions like “Is there any Facebook SDK for Xamarin.Forms? Because there is one for Android Xamarin Native.”. This kind of questions can apply to different SDK that are compatible only with specific platforms and cannot be consumed directly in Xamarin.Forms. The answer to these questions usually is “If there is an available native Xamarin SDK it can be consumed in your Xamarin.Forms project. All you have to do is to create an abstraction like you would do with any other platform specific code.”.

As you might already understand, in this article we will create an abstraction over Xamarin.Facebook.Android and Xamarin.Facebook.iOS in order to display a native Facebook Login Button and handle the authentication related events in our Xamarin.Forms application.

Continue reading “Using Native Facebook Login Button in Xamarin.Forms”