Xamarin - Facebook登录问题

时间:2016-12-28 05:18:39

标签: ios facebook facebook-graph-api xamarin.forms

我正在使用Xamarin.forms PCL androidIOS项目

问题

  • 登录设置 - > Facebook - >添加A

  • 的登录凭据
  • Facebook App - 登录 - >添加B

  • 的登录凭据
  • 当我使用Facebook APP登录我的应用程序时,则需要登录A.

我想使用Facebook登录B.

当我使用其他帐户登录时,我必须转到设置 - > Facebook和更改凭证

以下是代码

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);
    }

请建议我如何通过Facebook应用程序登录facebook用户详细信息,而不是通过设置 - &gt; facebook - &gt;登录

提前致谢!

0 个答案:

没有答案