Facebook login webview

BlackShawarnaBlackShawarna ✭✭ITUniversity ✭✭

Hi all!
I'm trying to connect my app using the component Facebook Android SDK. I configured the facebook app and using it with the example HelloFacebookSample inside the component everything works. I set the facebook app id in the string.xml and call it in the Activity using metadata.
Now i've got to integrate the login into my app and i don't understand why, after the facebook's button click, my app crashes.
Time to post some code.

This is my main.axml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:facebook="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/login_layout" android:orientation="vertical"> <com.facebook.widget.LoginButton android:text="accedi tramite Facebook" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnFacebook" android:padding="40px" android:background="#4dffffff" android:textColor="#ffffffff" android:textSize="15dp" facebook:confirm_logout="false" facebook:fetch_user_info="true" /> </LinearLayout> <FrameLayout android:id="@+id/fragment_container" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>

in my MainActivity.cs i've got this code:

[assembly:MetaData ("com.facebook.sdk.ApplicationId", Value ="@string/app_id")]

namespace FacebookLogin
{
    [Activity (Label = "FacebookLogin", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : FragmentActivity
    {
        LoginButton loginButton;
        private UiLifecycleHelper uiHelper;
        private IGraphUser user;
        private Session.IStatusCallback callback;
        private ProfilePictureView profilePictureView;

        public MainActivity ()
        {
            callback = new MyStatusCallback (this);
        }

        class MyStatusCallback : Java.Lang.Object, Session.IStatusCallback
        {
            MainActivity owner;

            public MyStatusCallback (MainActivity owner)
            {
                this.owner = owner;
            }

            public void Call (Session session, SessionState state, Java.Lang.Exception exception)
            {
                owner.OnSessionStateChange (session, state, exception);
            }
        }

        class MyUserInfoChangedCallback : Java.Lang.Object, LoginButton.IUserInfoChangedCallback
        {
            MainActivity owner;

            public MyUserInfoChangedCallback (MainActivity owner)
            {
                this.owner = owner;
            }

            public void OnUserInfoFetched (IGraphUser user)
            {
                owner.user = user;
                owner.UpdateUI ();
            }
        }

        protected override void OnCreate (Bundle savedInstanceState)
        {
            base.OnCreate (savedInstanceState);
            uiHelper = new UiLifecycleHelper (this, callback);
            uiHelper.OnCreate (savedInstanceState);

            SetContentView(Resource.Layout.Main);

            loginButton = (LoginButton)FindViewById (Resource.Id.btnFacebook);
            loginButton.UserInfoChangedCallback = new MyUserInfoChangedCallback (this);
        }

        protected override void OnResume ()
        {
            base.OnResume ();
            uiHelper.OnResume ();
            UpdateUI ();
        }

        protected override void OnSaveInstanceState (Bundle outState)
        {
            base.OnSaveInstanceState (outState);
            uiHelper.OnSaveInstanceState (outState);
        }

        protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult (requestCode, resultCode, data);
            uiHelper.OnActivityResult (requestCode, (int)resultCode, data);
        }

        protected override void OnPause ()
        {
            base.OnPause ();
            uiHelper.OnPause ();
        }

        protected override void OnDestroy ()
        {
            base.OnDestroy ();
            uiHelper.OnDestroy ();
        }

        private void OnSessionStateChange (Session session, SessionState state, Exception exception)
        {
            UpdateUI ();
        }

        private void UpdateUI ()
        {
            Session session = Session.ActiveSession;
            bool enableNews = (session != null && session.IsOpened);

            if (enableNews && user != null) {
                //Get user info
                Log.Debug ("Name", user.FirstName);
                Log.Debug ("Surname", user.LastName);
                Log.Debug ("Birthday", user.Birthday);
                Log.Debug ("Username", user.Username);
                Log.Debug ("Gender", user.GetProperty("gender").ToString());
            Log.Debug ("Email", user.GetProperty("email").ToString());      //See how to get it
                profilePictureView.ProfileId = user.Id;
            } else {
                //Return to Login
                //StartActivity (typeof(MainActivity));
            }
        }
    }

I know i'm missing a step. For example, how did the program know when to open the webview for Facebook login? (If i haven't the app installed in my smartphone). Is the fragmentManager an essential part?
I'm using the same code of the HelloFacebookSample and in that example it works so any help is really appreciated.

Thanks men.

Posts

Sign In or Register to comment.