在 iOS 上的 NSUserDefaults 中保存 Facebook access_token

发布于 2024-11-07 07:37:37 字数 854 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城凉梦凉人心 2024-11-14 07:37:37

这里的代码不是同步的。这意味着它在调用 [facebookauthorize:nil delegate:self]; 后不会阻塞。您应该实现 fbDidLogin 委托方法,以便在用户实际成功登录时收到通知。此时,检索访问令牌并将其保存到用户默认值。

这是部分示例:

- (void)userClickedFacebookLogin {
    [facebook authorize:nil delegate:self]; // delegate is self
}

// Delegate method that you should implement to get notified
// when user actualy logs in.
- (void)fbDidLogin {
    // now get the access token and save to user defaults
    NSString *access = [facebook accessToken];
    // ..
}

还要确保具有上述代码的类至少实现 FBSessionDelegate 协议。

@interface MyClass <FBSessionDelegate> {

}

@end

查看 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 the fbDidLogin 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:

- (void)userClickedFacebookLogin {
    [facebook authorize:nil delegate:self]; // delegate is self
}

// Delegate method that you should implement to get notified
// when user actualy logs in.
- (void)fbDidLogin {
    // now get the access token and save to user defaults
    NSString *access = [facebook accessToken];
    // ..
}

Also make sure that the class which has the above code implements the FBSessionDelegate protocol at minimum.

@interface MyClass <FBSessionDelegate> {

}

@end

Look at the DemoApp sample and specifically the DemoAppViewController class from Facebook to get a better idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文