how to avoid logging users out when migrating from iOS Facebook 2.x -> 3.x

穿精又带淫゛_ 提交于 2019-12-21 05:22:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!