Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Facebook Native Login Using Forms

$
0
0

Using the example in Facebook SDK package which is Android project (not Forms) I created a page renderer for facebook login.
I got the facebook permission dialog open but no matter what I click, Cancel or OK, the callback handler is not called.
Maybe I need to set the activity in some other way?

    public class LoginPageR : ContentPage
    {
        public LoginPageR ()
        {

        }
    }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Facebook;
using Xamarin.Facebook.Login;

[assembly: Xamarin.Forms.ExportRenderer (typeof (Chatter.LoginPageR), typeof (Chatter.Droid.LoginPageRenderer))]

namespace Chatter.Droid
{
    public class LoginPageRenderer : PageRenderer
    {
        ICallbackManager callbackManager;

        protected override void OnElementChanged (ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged (e);

            System.Diagnostics.Debug.WriteLine ("initfacebook");

            var activity = this.Context as Activity;
            FacebookSdk.SdkInitialize (activity);

            callbackManager = CallbackManagerFactory.Create ();

            var loginCallback = new FacebookCallback<LoginResult> {
                HandleSuccess = loginResult => {
                    System.Diagnostics.Debug.WriteLine ("fblogin");
                    UpdateUI ();
                },
                HandleCancel = () => {
                    System.Diagnostics.Debug.WriteLine ("fbcancel");
                    UpdateUI ();
                },
                HandleError = loginError => {
                    System.Diagnostics.Debug.WriteLine ("fberror");
                    UpdateUI ();
                }
            };

            LoginManager.Instance.RegisterCallback (callbackManager, loginCallback);

            LoginManager.Instance.LogInWithReadPermissions (activity, new List<string> ());
        }

        private void UpdateUI ()
        {
            var token = AccessToken.CurrentAccessToken != null;
            var profile = Profile.CurrentProfile;

            if (token && profile != null) {
                //profilePictureView.ProfileId = profile.Id;
                System.Diagnostics.Debug.WriteLine ("updateui " + profile.FirstName);
            } else {
                //profilePictureView.ProfileId = null;
                System.Diagnostics.Debug.WriteLine ("updateui");
            }
        }

        class FacebookCallback<TResult> : Java.Lang.Object, IFacebookCallback where TResult : Java.Lang.Object
        {
            public Action HandleCancel { get; set; }
            public Action<FacebookException> HandleError { get; set; }
            public Action<TResult> HandleSuccess { get; set; }

            public void OnCancel ()
            {
                var c = HandleCancel;
                if (c != null)
                    c ();
            }

            public void OnError (FacebookException error)
            {
                var c = HandleError;
                if (c != null)
                    c (error);
            }

            public void OnSuccess (Java.Lang.Object result)
            {
                var c = HandleSuccess;
                if (c != null)
                    c (result.JavaCast<TResult> ());
            }
        }

    }
}

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>