第一步 產生xmppstream對象 並且設定委託
_xmppStream = [[XMPPStreamalloc] init];
[_xmppStreamaddDelegate:selfdelegateQueue:dispatch_get_main_queue()];
第二步 設定登陸帳戶名稱字與伺服器名字
NSString *userId = @"test1@mini.local";//登陸用的使用者名稱字
NSString *server = @"mini.local";//@"127.0.0.1"; //用伺服器名字或者IP地址,推薦用IP地址,在用伺服器名字時候自己錯誤的用成了admin@mini.local(伺服器登陸帳戶名稱字)搞了半天沒反映,確實很頭大
[_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