且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在iOS上的NSUserDefaults中保存Facebook access_token

更新时间:2023-02-20 12:49:37

此处的代码不同步。这意味着它不会在调用 [facebook authorize:nil委托:self]; 之后阻塞。相反,您应该实现 fbDidLogin 委托方法,以在用户实际成功登录时收到通知。此时,获取访问令牌并将其保存为用户默认值。

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.

这里是部分示例:

- (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 协议。

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

@interface MyClass <FBSessionDelegate> {

}

@end

看在 DemoApp 示例,尤其是来自Facebook的 DemoAppViewController 类,以获得更好的主意。

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