Facebook Login Using Xamarin.Auth In Xamarin iOS

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available.
 

Prerequisites
  • Xamarin Studio(or)Visual Studio For Mac.
  • Xcode.
  • Facebook Account.
The steps given below are required to be followed in order to Facebook Login using Xamarin.Auth in Xamarin iOS.

Step 1

Go to https://developers.facebook.com.

 

Step 2

After Loggin in to Developer.Facebook.com, Click Add a New App.

 

Step 3

In this step, ceate your app Id.
Afterwards, click on "Create App ID".

 

Step 4

Now, your app id is ready. Go to Dashboard. App details are available here.You can copy your app id and paste your code.

 

Step 5

In this step, set valid OAuth redirect URI.
 
 

Step 6

Now Create Your App.

Go To Xamarin Studio.

Click New Solution—> select iOS—>select App--> Choose single View App. Afterwards, click Next.

 

Step 7

In this step, configure your app. Give the app name (Ex:sample), Organization Identifier. Afterwards, click Next.

 

Step 8

In this step, give your project name (Ex: Sample) and solution name (Ex: Sample). Give the path of your project. Afterwards, click Create.

 

Step 9

Subsequently, go to the solution. In the solution, get all the files and sources in your project. Now, select Main.storyboard and double click to open Main.storyboard page.

 

Step 10

After opening the Main.storyboard, you can design this page, as per your desire.



Step 11

In this step, You need to add Xamarin.Auth Package.

Go to Solution Explorer—> Packages—>Right Click—>Add Packages—>Select Xamarin.Auth.

 

Step 12

Now, check your package.

Go to Solution Explorer—> Packages—> Xamarin.Auth.

 

Step 13

In this step, design your app using Storyboard, Toolbox, and Properties.
  1. ImageView (imgCover)
  2. ImageView (imgProfile)
  3. Label(lblName)
  4. Button(btnLogin)
 

Step 14

In this step, go to the ViewController.cs page. Write the code given below.

ViewController.cs
  1. using System;  
  2. using UIKit;  
  3. using Xamarin.Auth;  
  4. using System.Json;  
  5. using Foundation;  
  6. namespace XamariniOSFacebookLogin {  
  7.     public partial class ViewController: UIViewController {  
  8.         protected ViewController(IntPtr handle): base(handle) {  
  9.             // Note: this .ctor should not contain any initialization logic.  
  10.         }  
  11.         public override void ViewDidLoad() {  
  12.             base.ViewDidLoad();  
  13.             // Perform any additional setup after loading the view, typically from a nib.  
  14.         }  
  15.         partial void UIButton8_TouchUpInside(UIButton sender) {  
  16.             var auth = new OAuth2Authenticator(clientId: “Your App Id“, scope: "", authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"), redirectUrl: new Uri("http://www.facebook.com/connect/login_success.html"));  
  17.             auth.Completed += Auth_Completed;  
  18.             var ui = auth.GetUI();  
  19.             PresentViewController(ui, truenull);  
  20.         }  
  21.         private async void Auth_Completed(object sender, AuthenticatorCompletedEventArgs e) {  
  22.             if (e.IsAuthenticated) {  
  23.                 var request = new OAuth2Request("GET"new Uri("https://graph.facebook.com/me?fields=name,picture,cover,birthday"), null, e.Account);  
  24.                 var response = await request.GetResponseAsync();  
  25.                 var user = JsonValue.Parse(response.GetResponseText());  
  26.                 var fbName = user["name"];  
  27.                 var fbCover = user["cover"]["source"];  
  28.                 var fbProfile = user["picture"]["data"]["url"];  
  29.                 lblName.Text = fbName.ToString();  
  30.                 imgCover.Image = UIImage.LoadFromData(NSData.FromUrl(new NSUrl(fbCover)));  
  31.                 imgProfile.Image = UIImage.LoadFromData(NSData.FromUrl(new NSUrl(fbProfile)));  
  32.             }  
  33.             DismissViewController(truenull);  
  34.         }  
  35.         public override void DidReceiveMemoryWarning() {  
  36.             base.DidReceiveMemoryWarning();  
  37.             // Release any cached data, images, etc that aren't in use.  
  38.         }  
  39.     }  
  40. }  


Step 15

Now, go to Run option, choose Debug with the desired simulator from the list of iPhone and iPad simulators. You can choose any one simulator and run it.

 

Output

After a few seconds, the app will start running on your iPhone simulator.You will see your app working successfully.

 

You can login using your Facebook login credentials. It will work successfully.

Summary

This was the process of how to implement Facebook Login using Xamarin.Auth in Xamarin iOS.

Up Next
    Ebook Download
    View all
    Learn
    View all