4

Upgrading our Facebook iOS integration from the 2.x SDK to 3.x SDK automatically logs users out who were previously logged in, since the authentication credentials that we used to have to manually handle ourselves are now handled behind-the-scenes by the new SDK.

Is there any way to force the 3.x SDK to authenticate using the access token and expiration date that we were previously manually storing, as a one-time authentication migration?

Thanks in advance!

Ed Koster
  • 101
  • 4

2 Answers2

4

Finally figured it out. The solution involves using the FBSessionTokenCachingStrategy object they provide, and specifically an FBSessionManualTokenCachingStrategy:

if (isUserUpgrading) {
   FBSessionTokenCachingStrategy *strategy = [[[FBSessionManualTokenCachingStrategy alloc] initWithUserDefaultTokenInformationKeyName:nil] autorelease];
   strategy.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"FBSessionToken"];         // use your own UserDefaults key
   strategy.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"FBSessionExpiration"]; // use your own UserDefaults key
   FBSession *session = [[[FBSession alloc] initWithAppID:@"MY_APP_ID"                                    // use your own appId
                                              permissions:nil
                                          urlSchemeSuffix:nil
                                       tokenCacheStrategy:strategy] autorelease];
   [FBSession setActiveSession:session];
} else {
   [FBSession openActiveSessionWithReadPermissions:...];  // normal authentication
}
Ed Koster
  • 101
  • 4
0

For Facebook sdk 3.1.1 I had to create a new class FBSessionManualTokenCachingStrategy which is a subclass of FBSessionTokenCachingStrategy and defines a fetchTokenInformation method as

   - (NSDictionary*)fetchTokenInformation; {
     return [NSDictionary dictionaryWithObjectsAndKeys:
        self.accessToken, FBTokenInformationTokenKey,
        self.expirationDate, FBTokenInformationExpirationDateKey,
        nil];
   }
Dare2Dream
  • 474
  • 5
  • 14