4
votes

I'm developing "Log in with Facebook" support in my game with the official Unity Facebook SDK. Actually I'm using the beta version 6.1 (but this problem where also in the stable version, 6.0).

A user can log in successfully and then I can make FB api requests. The problem is that FB SDK isn't saving the session, since when you close the app and launch it again, the FB.IsLoggedIn value is false.

It doesn't work in any platform: Editor, iOS & Android.

Note: The Status property in Facebook Settings is already checked.

Here's how I implemented it:

FacebookController.cs

void Awake()
{
    if (instance == null)
    {
        DontDestroyOnLoad(gameObject);
        instance = this;

        FB.Init(OnFacebookInit);
    } else if (instance != this)
    {
        Destroy(gameObject);
    }
}

void OnFacebookInit()
{
    if (FB.IsLoggedIn)
    {
        FBIsLoggedIn();
    }
}

public void LoginWithFacebook(OnUserAuthenticated _userAuthCallback)
{
    userAuthCallback = _userAuthCallback;
    FB.Login("email, user_friends", AuthCallback);
}

void AuthCallback(FBResult result)
{
    if (FB.IsLoggedIn)
    {
        FBIsLoggedIn();
    } else
    {
        Debug.Log("User cancelled login");
    }
}

void FBIsLoggedIn()
{
    FB.API("me", Facebook.HttpMethod.GET, UserCallBack);
    FB.API("me/friends", Facebook.HttpMethod.GET, FriendsCallback);
}

Do I need to add some code to make FB save the valid session?

Thanks in advance!

2
Any success on this yet? I'm having the same issue.Ryan H.
Hi Ryan, yes! My problem was that I expected facebook to save the session on the editor, when it doesn't. Try to test it on the specific platform, and make sure you're receiving visual feedback if user is logged in. In my previous example, i.e. try to disable fb log in button in the UserCallback method.Arol
Thanks Arol. I deployed to an iPad and it works as expected. Too bad it doesn't work in the editor too.Ryan H.

2 Answers

1
votes

My problem was that I expected facebook to save the session on the editor, when it doesn't.

When you build it into the device, FB SDK remembers successfully the last valid session. In my example, my error was that I didn't give feedback to the user interface. The FB.IsLoggedIn returned true, but I solved it by changing the state of the FB login button in my UserCallBack method.

Hope it helps you!

0
votes

I have the same problem with my login system. However< I solved it calling the same function when the user clicks "login with Facebook". Then I saved a variable in the Unity Editor's dictionary to know that was a Facebook login:

PlayerPrefs.SetString("ISFACEBOOK", true);

Finally I added a small check in the FB.Init() to check if the editor has that key.

private void InitCallback () { if (FB.IsInitialized) { FB.ActivateApp(); Debug.Log ("[Initcallback] activateApp");

        if (true == PlayerPrefs.HasKey("ISFACEBOOK") {
            //normal login
            FacebookLoginButton();
        }
    } 
    else 
    {
        SetMessage("Failed to Initialize the Facebook SDK");
    }

If the user logs out I delete the variable from the editor.

I know that is a cheap way to solve the problem but Facebbok API requires to call either to FB.LogInWithPublishPermissions or FB.LogInWithReadPermissions.

Hope this helps you.