大约 6 个月前,我从这个链接下载了 robbiehanson/XMPPFramework:https ://github.com/robbiehanson/XMPPFramework 。我按照入门部分中提到的步骤进行操作。它没有抛出任何错误。只需尝试按照这些步骤为您的应用程序设置 xmppframework。
在示例应用程序中,我找到了setupStream()
启动应用程序时调用的函数。在这个函数中,我正在创建一个 xmppStream 并激活我的应用程序中需要的不同模块。例如
xmppStream = [[XMPPStream alloc] init];
// Activate xmpp modules after creating them
[xmppReconnect activate:xmppStream];
[xmppRoster activate:xmppStream];
[xmppvCardTempModule activate:xmppStream];
[xmppvCardAvatarModule activate:xmppStream];
[xmppCapabilities activate:xmppStream];
// Add ourself as a delegate to anything we may be interested in
[xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
[xmppStream setHostName:XMPPHOST];
[xmppStream setHostPort:5222];
// You may need to alter these settings depending on the server you're connecting to
allowSelfSignedCertificates = NO;
allowSSLHostNameMismatch = NO;
设置流后,您需要像这样进行身份验证:
- (BOOL)connect:(NSString *)myJID //username registered with server
{
if (![xmppStream isDisconnected]) {
return YES;
}
if (myJID == nil) {
return NO;
}
[xmppStream setMyJID:[XMPPJID jidWithString:myJID]];
NSError *error = nil;
if (![xmppStream connect:&error])
{
if(DEBUG)
{
NSLog(@"ERROR: Not connected to XMPP Server");
}
DDLogError(@"Error connecting: %@", error);
return NO;
}
return YES;
}
该函数将被框架调用并在此处传递密码:
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
if(sender == xmppStream)
{
//DDLogVerbose(@"In xmppStream: %@: %@", THIS_FILE, THIS_METHOD);
isXmppConnected = YES;
NSError *error = nil;
if (![[self xmppStream] authenticateWithPassword:password error:&error])
{
DDLogError(@"Error authenticating: %@", error);
}
}
}
现在,如果用户通过身份验证,将调用此函数:
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
if(sender == xmppStream)
{
[self goOnline];
}
}
goOnline 会将用户的存在发送到服务器:
- (void)goOnline
{
XMPPPresence *presence = [XMPPPresence presence]; // type="available" is implicit
[xmppStream sendElement:presence];
}
现在您可以发送/接收消息/状态等。