我一直在使用旧的Facebook SDK,并将accessToken
和expirationDate
保存在NSUserDefaults
中。但是现在在新的Facebook SDK 3.2
中有一个名为FBAccessTokenData
的新类,我可以使用以下命令从其中获取accessToken:
[FBSession activeSession].accessTokenData.accessToken
[FBSession activeSession].accessTokenData.expirationDate
我的问题是,当我重新启动应用程序并再次单击Facebook按钮时,尽管我在3分钟前使用了该应用程序,登录Facebook并获得了新的 token ,但它仍去了Facebook要求新的 token 。因此 token 尚未保存。
我正在尝试将新的
accessToken
保存到[FBSession activeSession]
中,但是accessToken
和expirationDate
是只读属性。我现在已经阅读Facebook SDK文档好几次了,搜索了stackOverflow和网络,但仍然不知道如何执行此操作。
这是我的代码:
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
switch (state)
{
case FBSessionStateOpen:
{
// This is how i'm trying to save the token, like the old Facebook SDK.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[FBSession activeSession].accessTokenData.accessToken forKey:@"FBAccessTokenKey"];
[defaults setObject:[FBSession activeSession].accessTokenData.expirationDate forKey:@"FBExpirationDateKey"];
[defaults synchronize];
NSLog(@"FB: Session is open!");
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
{
NSLog(@"FB: Session is closed or login failed");
[self closeSession];
}
break;
default:
break;
}
[[NSNotificationCenter defaultCenter] postNotificationName:FBSessionStateChangedNotification
object:session];
if (error)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (void)openSession
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:
^(FBSession *session, FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
- (void)closeSession
{
[FBSession.activeSession closeAndClearTokenInformation];
}
提前致谢!
最佳答案
因此,在进行更多搜索之后,我找到了答案。如果有人对新的Facebook SDK 3.2
accessToken
有问题,请查看此link。然后您会找到想要的东西。
基本上,您将创建一个处理 token 保存和加载的类。与将其保存到本地plist文件相比,这非常容易和简单。
请享用! :)
关于ios - 无法在SDK 3.2中保存Facebook accessToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15266391/