Delpin Susai Raj Monday 2 July 2018

Facebook Login Using Xamarin.Auth in Xamarin iOS

In this blog, you will learn how to Facebook Login Using Xamarin.Auth in Xamarin iOS, using Xamarin Studio.





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, using Xamarin Studio.


Step 1

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



Step 2

After Login Developer.Facebook.com, Click Add a New App.



Step 3

In this step Create your app Id.

Display Name:Ex: MyApp

Contact Email  : someone@example.com

Afterwards, click  Create App ID.



Step 4

Now, your app id is ready, go to Dashboard the app details is available here.you can copy your app id and paste your code.



Step 5

In this step set Valid OAuth redirect URI.

http://www.facebook.com/connect/login_success.html




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

using System;

using UIKit;
using Xamarin.Auth;
using System.Json;
using Foundation;

namespace XamariniOSFacebookLogin
{
 public partial class ViewController : UIViewController
 {
  protected ViewController(IntPtr handle) : base(handle)
  {
   // Note: this .ctor should not contain any initialization logic.
  }

  public override void ViewDidLoad()
  {
   base.ViewDidLoad();

   // Perform any additional setup after loading the view, typically from a nib.
  }



  partial void UIButton8_TouchUpInside(UIButton sender)
  {
   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"));

   auth.Completed+= Auth_Completed;
   var ui = auth.GetUI();
   PresentViewController(ui, true, null);
  }

  private async void Auth_Completed(object sender, AuthenticatorCompletedEventArgs e)
  {
   if (e.IsAuthenticated)
   {
    var request = new OAuth2Request("GET", 
                                    new Uri("https://graph.facebook.com/me?fields=name,picture,cover,birthday")
                                    , null, e.Account);
    var response = await request.GetResponseAsync();
    var user = JsonValue.Parse(response.GetResponseText());
    var fbName = user["name"];
    var fbCover = user["cover"]["source"];
    var fbProfile = user["picture"]["data"]["url"];

    lblName.Text = fbName.ToString();
    imgCover.Image = UIImage.LoadFromData(NSData.FromUrl(new NSUrl(fbCover)));
    imgProfile.Image = UIImage.LoadFromData(NSData.FromUrl(new NSUrl(fbProfile)));
   }
            DismissViewController(true, null);
  }

  public override void DidReceiveMemoryWarning()
  {
   base.DidReceiveMemoryWarning();
   // Release any cached data, images, etc that aren't in use.
  }
 }
}





Step 15

Now, go to Run option , choose Debug and the list of iPhone and iPad simulators, which are available. 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 credential , It will work successfully.



Summary

This was the process of how to Facebook Login Using Xamarin.Auth in Xamarin iOS , using Xamarin Studio.


3 comments:

  1. If you are looking for more information about flat rate locksmith Las Vegas check that right away. hotmail

    ReplyDelete
  2. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Advanced Scrum Master during this lockdown period everyone can use it wisely.
    Advanced Scrum Master Training Online

    ReplyDelete
  3. The blog has been written in a manner that there isn't anything that has been left revealed, and furthermore, I have perused different web journals that are posted here and they are altogether worth a read.
    visit site

    ReplyDelete