Question: Xamarin - Facebook Login Issue

Question

Xamarin - Facebook Login Issue

Answers 0
Added at 2016-12-28 06:12
Tags
Question

I am using Xamarin.forms PCL having android and IOS project

Issue:

  • Login in setting -> Facebook -> add login credential of A

  • Facebook App - Login -> add login credential of B

  • When I login through my app with Facebook APP, then it take login of A.

I want to use Facebook login of B.

When I have login with another account then I have to go to setting -> Facebook and change credential

Following is the code

public class FacebookLoginButtonRenderer : ButtonRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            UIButton button = Control;

            button.TouchUpInside += delegate {
                HandleFacebookLoginClicked();
            };
        }
    }
    private void HandleFacebookLoginClicked()
    {
        LoginManager manager = new LoginManager();
        manager.Init();
        manager.LogOut();
        manager.LoginBehavior = LoginBehavior.SystemAccount;

        manager.LogInWithReadPermissions(new string[] { "public_profile", "email", "user_birthday" }, delegate (LoginManagerLoginResult result, NSError error)
        {
            if (error != null)
            {
                App.OnFacebookAuthFailed();
                return;
            }
            else if (result.IsCancelled)
            {
                new UIAlertView("", "User Cancel Login", null, "Ok", null).Show();
                return;
            }
            else
            {
                var UserToken = result.Token.TokenString;
                if (UserToken != null)
                {
                    var request = new Facebook.CoreKit.GraphRequest("/me?fields=id,email,first_name,last_name,gender,birthday", null, UserToken, null, "GET");
                    request.Start((connection, result1, error1) =>
                    {
                        if (error1 != null)
                        {
                            new UIAlertView("Error...", error.Description, null, "Ok", null).Show();
                            return;
                        }
                        var obj = result1 as NSDictionary;
                        FBModel fb = new FBModel();

                        fb.ID = obj["id"].ToString();
                        fb.Name = obj["first_name"].ToString();
                        fb.Email = obj["email"].ToString();
                        fb.LastName = obj["last_name"].ToString();
                        fb.Gender = obj["gender"].ToString();
                        if (obj.ContainsKey(new NSString("birthday")))
                        {
                            var birthdate = obj["birthday"].ToString();
                            fb.Age = DateTime.Today.Year - Convert.ToInt32(birthdate.Split('/')[2]);
                        }
                        else
                        {
                            fb.Age = 25;
                        }

                        App.OnFacebookIOSAuthSuccess(fb);
                    });

                }
            }
        });
    }
}

AppDelegate.cs

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        Xamarin.FormsMaps.Init();
        LoadApplication(new App());

        Settings.AppID = APPID;
        Settings.DisplayName = Name;
        //...

        // This method verifies if you have been logged into the app before, and keep you logged in after you reopen or kill your app.
        return base.FinishedLaunching(app, options);
    }

    public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
    {
        // We need to handle URLs by passing them to their own OpenUrl in order to make the SSO authentication works.
        return ApplicationDelegate.SharedInstance.OpenUrl(application, url, sourceApplication, annotation);
    }

Please suggest me how can I get facebook user detail login through facebook app not through setting -> facebook -> login

Thanks in advance!

Answers to

Xamarin - Facebook Login Issue

Source Show
◀ Wstecz