问题
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!
回答1:
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
}
回答2:
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]; }
来源:https://stackoverflow.com/questions/13260141/how-to-avoid-logging-users-out-when-migrating-from-ios-facebook-2-x-3-x