我一直在使用舊的Facebook SDK並在NSUserDefaults
中保存accessToken
和expirationDate
。但是,現在在新的Facebook SDK 3.2
有一個新的類名爲FBAccessTokenData
,我可以從那裏通過採取的accessToken:無法在SDK 3.2中保存Facebook accessToken
[FBSession activeSession].accessTokenData.accessToken
[FBSession activeSession].accessTokenData.expirationDate
我的問題是,當我重新啓動我的應用程序,並再次點擊Facebook的按鈕,它會向Facebook要求提供新的令牌,儘管我在3分鐘前使用了該應用程序,登錄Facebook並獲得了新的令牌。所以令牌沒有被保存。
我想新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];
}
在此先感謝!