我正在尝试创建一个实现 Facebook Chat 的应用程序。据我所知,我已经正确设置了所有 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
。此外,从未调用任何其他委托(delegate)方法,包括 xmppStreamDidAuthenticate
。我做错了什么?
最佳答案
我终于找到答案了!!在这里,以防其他人遇到与我相同的问题:
当调用 openActiveSessionWithReadPermissions:allowLoginUI:completionHandler:
时,FBSession
对象实际上并不与 Facebook 服务器通信或尝试与它们进行身份验证,它只是加载先前的 authenticationToken。在我的例子中,这个 token 已经失效,但我没有意识到,也没有什么可以告诉我的。我终于通过记录 token 并将其放入 Facebook 的 Access Token Debugger 中解决了这个问题。 .要检查您的 token 是否有效,您必须调用 [FBSession renewSystemCredentials:]
并等待结果。然后您可以确定在尝试创建新 token 之前是否需要手动 closeAndClearTokenInformation
。
关于iphone - XMPP 身份验证返回是,但从未调用 XMPPStreamDidAuthenticate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17217672/