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.
Please note that this technique can be applied to any Xamarin native SDKs.
Let’s create our PCL Xamarin.Forms project. It should have 2 targets: Android and iOS. I would highly recommend to switch to .netstandard 2.0 and update all the NuGet packages. Don’t forget that you Android project should target Android 8.1 without it the NuGet update will fail.
Facebook
Now we have to create a Facebook application on https://developers.facebook.com/. Once created we have to add a “Facebook Login” product. Then we have to create 2 platforms for our application Android and iOS, it can be easily done if you will follow the Quickstart instructions per platform, you can ignore all the steps involving code or configuration files manipulations. Please pay attention to your package name and bundle identifier, they should match your Android and iOS projects. For Android you will have to provide a “Key Hashes”, luckily the Quickstart instructions are nicely explaining how to achieve that. I am on purpose not going into details since it is a very straightforward process, thumbs up for the Facebook team. Also you can easily generate test users for your application on Facebook under Roles > Test Users.
It’s time to write some code!
Xamarin.Forms
Let’s create our abstraction layer, it will be a very simple Xamarin.Forms control:
This View is exposing the next properties:
Permissions - array of permission to be asked from the FB user account
OnSuccess - a Command that will return the authentication token and execute when the authentication process will complete
OnError - a Command that will return the error description and execute when an exception will be thrown by the authentication process
OnCancel - a Command that will execute when the user will manually cancel the authentication process
Now create a ViewModel and set it as a BindingContext of you main / root Page with a FacebookLoginButton on it:
iOS
Add Xamarin.Facebook.iOS NuGet package.
Add the next lines to your Info.plist, replacing %APP_ID% by your FB app id:
Your AppDelegate.cs should override the OpenUrl method:
Create a FacebookLoginButtonRenderer:
Android
Add Xamarin.Facebook.Android NuGet package.
Create a strings.xml under Resources/values, don’t forget to replace %APP_ID% by FB app id & %APP_NAME% by your Android application name:
In your AndroidManifest.xml add Internet permission.
Add the next lines to your AndroidManifest.xml inside the application tag:
Add a CallbackManager to your MainActivity.cs and override OnActivityResult:
Create a FacebookLoginButtonRenderer:
Conclusion
Our abstraction is a simple custom View that is rendered on different platforms via custom Renderers. The rendering is pretty straightforward, we create an instance of a native FacebookLoginButton and we listen to platform specific authentication related events to fire our own XF events.
It is a fairly simple solution that demonstrates that you don’t have to avoid using Xamarin Native SDKs just because they are not supported out-of-the-box on Xamarin.Forms.