What would be the proper way to handle OAuth access token between two sessions?
In Facebook's SDK example they don't even bother. The documentation says that if user has official Facebook application on his device and if device is capable of multitasking then the "magic" in Facebook application will take care of everything.
But what if user doesn't have or even doesn't want the Facebook app on his device? Should I save access token myself in NSUserDefaults? I found example in Facebook Application Development For Dummies, but they store only facebook.accessToken so the thing wasn't working properly at first. Then I played with the code for a while. After looking inside a few of Facebook SDK methods I decided to store facebook.expirationDate too and now the code seem to work.
But googling around didn't confirm it as a smart long term solution so I'm asking here if someone has more experience with Facebook development. Should I store accessToken/expirationDate or not?
EDIT - Here's the code I have now:
And, here is the method from the book, bold lines added by me:
In Facebook's SDK example they don't even bother. The documentation says that if user has official Facebook application on his device and if device is capable of multitasking then the "magic" in Facebook application will take care of everything.
But what if user doesn't have or even doesn't want the Facebook app on his device? Should I save access token myself in NSUserDefaults? I found example in Facebook Application Development For Dummies, but they store only facebook.accessToken so the thing wasn't working properly at first. Then I played with the code for a while. After looking inside a few of Facebook SDK methods I decided to store facebook.expirationDate too and now the code seem to work.
But googling around didn't confirm it as a smart long term solution so I'm asking here if someone has more experience with Facebook development. Should I store accessToken/expirationDate or not?
EDIT - Here's the code I have now:
Code:
//Called when the user successfully logged in.
- (void)fbDidLogin
{
NSLog (@"did login");
[[NSUserDefaults standardUserDefaults] setObject:facebook.accessToken
forKey:kFacebookAccessToken];
[[NSUserDefaults standardUserDefaults] setObject:facebook.expirationDate
forKey:kFacebookExpirationDate];
...
}
//Called when the user logged out.
- (void)fbDidLogout
{
NSLog (@"did logout");
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kFacebookAccessToken];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kFacebookExpirationDate];
...
}
And, here is the method from the book, bold lines added by me:
Code:
- (NSString *)facebookAccessToken
{
if (facebook.accessToken)
return (facebook.accessToken);
NSString *savedToken = [[NSUserDefaults standardUserDefaults] objectForKey:kFacebookAccessToken];
if (savedToken) {
facebook.accessToken = savedToken;
[B]facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:kFacebookExpirationDate];
if (![facebook isSessionValid])
facebook.accessToken = nil;[/B]
}
return (facebook.accessToken);
}
Last edited:
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.