我正在尝试创建一个实现Facebook聊天的应用程序。据我所知,我已经正确地设置了所有XMPP的东西,但是我不能让它工作。
在用户登录并通过身份验证到Facebook(通过FBSession
)后,我尝试连接到聊天服务。这就是XMPP的用武之地:
-(void)connect
{
[self setupStream];
NSError *err;
[self.xmppStream connectWithTimeout:10.00 error:&err];
}
-(void)setupStream
{
_xmppStream = [[XMPPStream alloc] initWithFacebookAppId:FACEBOOK_APP_ID];
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
- (void)xmppStreamDidConnect:(XMPPStream *)sender {
NSError *error;
NSError *err;
[self.xmppStream secureConnection:&err];
bool authed = [self.xmppStream authenticateWithFacebookAccessToken: FBSession.activeSession.accessTokenData.accessToken error:&error];
NSLog(@"%@", err);
NSLog(@"%@", [self.xmppStream authenticationDate]);
NSLog(@"%d, %@", authed, error);
}
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
NSLog(@"did authenticate");
[self goOnline];
}
运行上面的命令时,一切似乎都很好:xmppStreamDidConnect
在短暂等待后被调用,authed
总是返回YES,它的错误总是null
。
然而,secureConnection
返回Error Domain=XMPPStreamErrorDomain Code=1 "Please wait until the stream is connected."UserInfo=0xb23dc30 {NSLocalizedDescription=请等待流连接成功。} authenticationDate
也总是null
。此外,没有调用任何其他委托方法,包括xmppStreamDidAuthenticate
。我做错了什么?
我终于找到我的答案了!这里是,以防其他人遇到和我一样的问题:
调用openActiveSessionWithReadPermissions:allowLoginUI:completionHandler:
时,FBSession
对象实际上并不与Facebook服务器通信或尝试与它们进行身份验证,它只是加载先前的authenticationToken。在我的例子中,这个令牌已经失效,但我没有意识到这一点,也没有任何东西可以告诉我。我最终通过记录令牌并将其放在Facebook的访问令牌调试器中来解决这个问题。要检查令牌是否有效,必须调用[FBSession renewSystemCredentials:]
并等待结果。然后,您可以确定在尝试创建新令牌之前是否需要手动closeAndClearTokenInformation
。