本文連結:http://www.cocoachina.com/ios/20141219/10703.html
XMPPFramework是一個OS X/iOS平臺的開源專案,使用Objective-C實現了XMPP協議(RFC-3920),同時還提供了用於讀寫XML的工具,大大簡化了基於XMPP的通訊應用的開發
1. 登入和好友上下線
1.1XMPP中常用物件們
XMPPStream:xmpp基礎服務類
XMPPRoster:好友列表類
XMPPRosterCoreDataStorage:好友列表(使用者賬號)在core data中的操作類
XMPPvCardCoreDataStorage:好友名片(暱稱,簽名,性別,年齡等資訊)在core data中的操作類
XMPPvCardTemp:好友名片實體類,從資料庫裡取出來的都是它
xmppvCardAvatarModule:好友頭像
XMPPReconnect:如果失去連線,自動重連
XMPPRoom:提供多使用者聊天支援
XMPPPubSub:釋出訂閱
1.2登入操作,也就是連線xmpp伺服器
- (void)connect {
if (self.xmppStream == nil) {
self.xmppStream = [[XMPPStream alloc] init];
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
if (![self.xmppStream isConnected]) {
NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
XMPPJID *jid = [XMPPJID jidWithUser:username domain:@"lizhen" resource:@"Ework"];
[self.xmppStream setMyJID:jid];
[self.xmppStream setHostName:@"10.4.125.113"];
NSError *error = nil;
if (![self.xmppStream connect:&error]) {
NSLog(@"Connect Error: %@", [[error userInfo] description]);
}
}
}
connect成功之後會依次呼叫XMPPStreamDelegate的方法,首先呼叫
- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
- (void)xmppStreamDidConnect:(XMPPStream *)sender
在該方法下面需要使用xmppStream 的authenticateWithPassword方法進行密碼驗證,成功的話會響應delegate的方法,就是下面這個
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
1.3上線
實現 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender 委託方法
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
[self.xmppStream sendElement:presence];
}
1.4退出並斷開連線
- (void)disconnect {
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
[self.xmppStream sendElement:presence];
[self.xmppStream disconnect];
}
1.5好友狀態
獲取好友狀態,通過實現
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
...
presence 的狀態:
available 上線
away 離開
do not disturb 忙碌
unavailable 下線
2. 接收訊息和傳送訊息
2.1接收訊息
通過實現
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message;
方法
當接收到 message 標籤的內容時,XMPPFramework 框架回撥該方法
根據 XMPP 協議,訊息體的內容儲存在標籤 body 內
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
NSString *messageBody = [[message elementForName:@"body"] stringValue];
}
2.2傳送訊息
傳送訊息,我們需要根據 XMPP 協議,將資料放到標籤內,
- (void)sendMessage:(NSString *) message toUser:(NSString *) user {
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message];
NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
NSString *to = [NSString stringWithFormat:@"%@@example.com", user];
[message addAttributeWithName:@"to" stringValue:to];
[message addChild:body];
[self.xmppStream sendElement:message];
}
3. 獲取好友資訊和刪除好友
3.1好友列表和好友名片
[_xmppRoster fetchRoster];//獲取好友列表
//獲取到一個好友節點
- (void)xmppRoster:(XMPPRoster *)sender didRecieveRosterItem:(NSXMLElement *)item
//獲取完好友列表
- (void)xmppRosterDidEndPopulating:(XMPPRoster *)sender
//到伺服器上請求聯絡人名片資訊
- (void)fetchvCardTempForJID:(XMPPJID *)jid;
//請求聯絡人的名片,如果資料庫有就不請求,沒有就傳送名片請求
- (void)fetchvCardTempForJID:(XMPPJID *)jid ignoreStorage:(BOOL)ignoreStorage;
//獲取聯絡人的名片,如果資料庫有就返回,沒有返回空,併到伺服器上抓取
- (XMPPvCardTemp *)vCardTempForJID:(XMPPJID *)jid shouldFetch:(BOOL)shouldFetch;
//更新自己的名片資訊
- (void)updateMyvCardTemp:(XMPPvCardTemp *)vCardTemp;
//獲取到一盒聯絡人的名片資訊的回撥
- (void)xmppvCardTempModule:(XMPPvCardTempModule *)vCardTempModule
didReceivevCardTemp:(XMPPvCardTemp *)vCardTemp
forJID:(XMPPJID *)jid