第一步 生成xmppstream对象 并且设置委托
_xmppStream = [[XMPPStreamalloc] init];
[_xmppStreamaddDelegate:selfdelegateQueue:dispatch_get_main_queue()];
第二步 设置登陆帐户名字与服务器名字
NSString *userId = @"[email protected]";//登陆用的用户名字
NSString *server = @"mini.local";//@"127.0.0.1"; //用服务器名字或者IP地址,推荐用IP地址,在用服务器名字时候自己错误的用成了[email protected](服务器登陆帐户名字)搞了半天没反映,确实很头大
[_xmppStreamsetMyJID:[XMPPJIDjidWithString:userId]];
[_xmppStream setHostName:server];
//启动连接服务器操作
NSError *error = nil;
bFlag = [_xmppStream connect:&error];
//启动连接操作后,回调函数(委托函数)
- (void)xmppStreamWillConnect:(XMPPStream *)sender将被调用,表示将要连接
- (void)xmppStreamDidConnect:(XMPPStream *)sender//登陆服务器成功
{
NSError *error = nil;
//验证帐户密码
NSString *password = @"test1";
BOOL bRes = [_xmppStream authenticateWithPassword:password error:&error];
}
//验证成功的回调函数
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
{
XMPPPresence *presence = [XMPPPresencepresence];
[[selfxmppStream] sendElement:presence];//发送上线通知
}
//验证失败的回调
- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error