Search code examples
facebookunity-game-enginefacebook-unity-sdk

facebook unity sdk 7.0.2 - FB.IsLoggedIn returns false when you reopen the app but should still be logged in


Prior to upgrading to the 7.X version of the Unity FB SDK, FB.IsLoggedIn would return true after FB.Init. Now, it is returning false and you have to do a new login every time.

This seems like a bug.

I'm on Unity 5.1.1p4 and building for Android. I haven't tried iOS yet.

Reproduction steps:

  1. start app, login to FB
  2. close app
  3. start app again, you will not be automatically logged in to FB (FB.IsLoggedIn is false)

Expected behavior:

FB.IsLoggedIn should be true if you were previously logged in. This is how it worked before.


Solution

  • In FBUnityInterface.mm:

    1. Add to - (id)init:

      [[FBSDKApplicationDelegate sharedInstance] application:[UIApplication sharedApplication] didFinishLaunchingWithOptions:NULL];
      
    2. Replace in - (void)configureAppId:

      [FBUnityUtility sendMessageToUnity:FBUnityMessageName_OnInitComplete userData:@{} requestId:0];
      

      with:

      if ([FBSDKAccessToken currentAccessToken]) {
          FBSDKAccessToken *token = [FBSDKAccessToken currentAccessToken];
          NSInteger expiration = token.expirationDate.timeIntervalSince1970;
          [FBUnityUtility sendMessageToUnity:FBUnityMessageName_OnInitComplete
                                    userData:@{
                                               @"opened" : @"true",
                                               @"access_token" : [FBSDKAccessToken currentAccessToken].tokenString,
                                               @"expiration_timestamp" : [@(expiration) stringValue],
                                               @"user_id" : [FBSDKAccessToken currentAccessToken].userID,
                                               @"permissions" : [token.permissions allObjects],
                                               }
                                   requestId:0];
          return;
      } else {
          [FBUnityUtility sendErrorToUnity:FBUnityMessageName_OnInitComplete errorMessage:@"Unknown login error" requestId:0];
      }