在 iOS 上的 NSUserDefaults 中保存 Facebook access_token
iOS初学者在这里。我正在使用以下代码在 NSUserDefaults 中保存我的 facebook accessToken 和过期日期:
facebook = [[Facebook alloc] initWithAppId:@"225083222506272"];
[facebook authorize:nil delegate:self];
NSString *access=[facebook accessToken];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:access, @"accessToken",[facebook expirationDate], @"expirationDate",nil];
[defaults registerDefaults:appDefaults];
我试图在稍后的调用中检索 accessToken 和过期日期:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *access=[defaults valueForKey:@"accessToken"];
NSDate *date=[defaults objectForKey:@"expirationDate"];
[facebook fbDialogLogin:access expirationDate:date];
但访问和日期为空。我做错了什么?
iOS beginner here. I'm using the following code to save my facebook accessToken and expirationDate in NSUserDefaults:
facebook = [[Facebook alloc] initWithAppId:@"225083222506272"];
[facebook authorize:nil delegate:self];
NSString *access=[facebook accessToken];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:access, @"accessToken",[facebook expirationDate], @"expirationDate",nil];
[defaults registerDefaults:appDefaults];
And I'm trying to retrieve accessToken and expirationDate in a later call with:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *access=[defaults valueForKey:@"accessToken"];
NSDate *date=[defaults objectForKey:@"expirationDate"];
[facebook fbDialogLogin:access expirationDate:date];
but access and date are null. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的代码不是同步的。这意味着它在调用
[facebookauthorize:nil delegate:self];
后不会阻塞。您应该实现fbDidLogin
委托方法,以便在用户实际成功登录时收到通知。此时,检索访问令牌并将其保存到用户默认值。这是部分示例:
还要确保具有上述代码的类至少实现 FBSessionDelegate 协议。
查看
DemoApp
示例,特别是DemoAppViewController< /code>
来自 Facebook 的课程以获得更好的想法。
The code here is not synchronous. It means it does not block after the call to
[facebook authorize:nil delegate:self];
. You should instead implement thefbDidLogin
delegate method to be notified of when the user has actually logged in successfully. At that point, retrieve the access tokens and save them to user defaults.Here's a partial sample:
Also make sure that the class which has the above code implements the
FBSessionDelegate
protocol at minimum.Look at the
DemoApp
sample and specifically theDemoAppViewController
class from Facebook to get a better idea.