iOS中XMPP簡單聊天實現 好友和聊天
好友和聊天流程圖
在看這篇文章之前,你需要配置好服務器,以及完成注冊和登錄的基本功能,才能繼續好友和聊天的操作。
下面兩篇文章是環境配置和注冊、登錄功能的詳細介紹:
XMPP的mysql和openfire環境配置
iOS中XMPP簡單聊天實現 注冊和登錄
另外必須了解一些CoreData相關知識
好友
-
點擊登錄之后,驗證成功就會跳到好友頁面。這個時候需要顯示你已經有的好友。
那么在tableViewCell中顯示好友姓名,需要數據源,數據源從服務器獲看你是否有好友,檢索到你的好友后把他顯示在列表上。
xmpp中管理好友的類是 XMPPRoster,并且使用coredata來儲存好友,達到數據持久化的效果。
那么我們可以將獲取儲存好友的倉庫和xmppRoster對象的初始化封裝在XMPPManager中。
在.h文件中聲明:- //好友管理
- @property(nonatomic,strong)XMPPRoster xmppRoster;
遵循代理:- @interface XMPPManager : NSObject<XMPPStreamDelegate,XMPPRosterDelegate>
在 .m文件中重寫init方法中:- //2.好友管理//獲得一個存儲好友的CoreData倉庫,用來數據持久化
- XMPPRosterCoreDataStorage rosterCoreDataStorage = [XMPPRosterCoreDataStorage sharedInstance];
- //初始化xmppRoster
- self.xmppRoster = [[XMPPRoster alloc]initWithRosterStorage:rosterCoreDataStorage dispatchQueue:dispatch_get_main_queue()];
- //激活
- [self.xmppRoster activate:self.xmppStream];
- //設置代理
- [self.xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
-
接收好友請求。
將接收到好友請求的方法也封裝在XMPPManager中:- // 收到好友請求執行的方法
- -(void)xmppRoster:(XMPPRoster )sender didReceivePresenceSubscriptionRequest:(XMPPPresence )presence{
- self.fromJid = presence.from;
- UIAlertView alert = [[UIAlertView alloc]initWithTitle:@"提示:有人添加你" message:presence.from.user delegate:self cancelButtonTitle:@"拒絕" otherButtonTitles:@"OK", nil];
- [alert show];
- }
- -(void)alertView:(UIAlertView )alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
- switch (buttonIndex) {
- case 0:
- [self.xmppRoster rejectPresenceSubscriptionRequestFrom:self.fromJid];
- break;
- case 1:
- [self.xmppRoster acceptPresenceSubscriptionRequestFrom:self.fromJid andAddToRoster:YES];
- break;
- default:
- break;
- }
- }
-
添加好友,添加的好友必須是服務器上存在的用戶,需要看對方是否同意。對方同意之后,刷新好友列表,顯示出來,同時在服務器上也要添加,這里服務器上用的是coredata來存儲個人的好友信息。
好友頁面實現文件,遵循代理,數據源數組
在viewDidLoad中完成初始化數組,設置代理和添加好友按鈕
這里簡化了添加好友,寫死了只能添加“張三”,如果需要添加更多,可以寫成借口
接下來是tableview數據源代理方法
tableview
這時候數組明顯是沒有jid對象的。獲取jid對象是在XMPPPRoster代理方法中實現的:
pragma mark xmppRoster 的代理方法
- pragma mark 開始檢索好友列表的方法
- -(void)xmppRosterDidBeginPopulating:(XMPPRoster *)sender{
- NSLog(@"開始檢索好友列表");
- }
- pragma mark 正在檢索好友列表的方法
- -(void)xmppRoster:(XMPPRoster )sender didRecieveRosterItem:(DDXMLElement )item{
- NSLog(@"每一個好友都會走一次這個方法");
- //獲得item的屬性里的jid字符串,再通過它獲得jid對象
- NSString jidStr = [[item attributeForName:@"jid"] stringValue];
- XMPPJID jid = [XMPPJID jidWithString:jidStr];
- //是否已經添加
- if ([self.rosterJids containsObject:jid]) {
- return;
- }
- //將好友添加到數組中去
- [self.rosterJids addObject:jid];
- //添加完數據要更新UI(表視圖更新)
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.rosterJids.count-1 inSection:0];
- [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
- }
- pragma mark 好友列表檢索完畢的方法
- -(void)xmppRosterDidEndPopulating:(XMPPRoster )sender{
- NSLog(@"好友列表檢索完畢");
- }
4. 刪除好友。列表刪除,數組刪除,服務器刪除。
- pragma mark 刪除好友執行的方法
- -(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath{
- if (editingStyle==UITableViewCellEditingStyleDelete) {
- //找到要刪除的人
- XMPPJID jid = self.rosterJids[indexPath.row];
- //從數組中刪除
- [self.rosterJids removeObjectAtIndex:indexPath.row];
- //從Ui單元格刪除
- [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic
- ];
- //從服務器刪除
- [[XMPPManager defaultManager].xmppRoster removeUser:jid];
- }
- }
5.進入聊天頁面
點擊進入聊天頁面的方法
聊天頁面接受jid值的屬性
聊天
1.發送普通文本消息
同樣在XMPPManager中進行封裝;
- //聊天信息歸檔
- @property(nonatomic,strong)XMPPMessageArchiving xmppMessageArchiving;
- //信息歸檔的上下文
- @property(nonatomic,strong)NSManagedObjectContext messageArchivingContext;
在init初始化時:
- //3.保存聊天記錄
- //初始化一個倉庫
- XMPPMessageArchivingCoreDataStorage *messageStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
- //創建一個消息歸檔對象
- self.xmppMessageArchiving = [[XMPPMessageArchiving alloc]initWithMessageArchivingStorage:messageStorage dispatchQueue:dispatch_get_main_queue()];
- //激活
- [self.xmppMessageArchiving activate:self.xmppStream];
- //上下文
- self.messageArchivingContext = messageStorage.mainThreadManagedObjectContext;
在聊天頁面的viewDidload中:
發送普通消息:
- -(void)doSend{
- //創建一個消息對象,并且指明接收者
- XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:self.chatToJid];
- //設置消息內容
- [message addBody:@"呵呵呵呵呵呵呵呵呵呵"];
- //發送消息
- [[XMPPManager defaultManager].xmppStream sendElement:message];
- //發送成功或者失敗,有兩種對應的代理方法
- }
消息發送是否成功,會走下面的代理方法:
xmppStream的代理方法
刷新消息的方法,需要熟悉CoreData知識
#pragma mark 刷新消息的方法
-(void)reloadMessage{
//得到上下文
NSManagedObjectContext context = [XMPPManager defaultManager].messageArchivingContext;
//搜索對象
NSFetchRequest request = [[NSFetchRequest alloc]init];
//創建一個實體描述
NSEntityDescription entity = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject" inManagedObjectContext:context];
[request setEntity:entity];
//查詢條件
NSPredicate pre = [NSPredicate predicateWithFormat:@"streamBareJidStr = %@ AND bareJidStr = %@",[XMPPManager defaultManager].xmppStream.myJID.bare,self.chatToJid.bare];
request.predicate = pre;
//排序方式
NSSortDescriptor sort = [[NSSortDescriptor alloc]initWithKey:@"timestamp" ascending:YES];
request.sortDescriptors = @[sort];
//執行查詢
NSError error = nil;
NSArray array = [context executeFetchRequest:request error:&error];
if (self.messages.count != 0) {
[self.messages removeAllObjects];
}
[self.messages addObjectsFromArray:array];
[self.tableView reloadData];
}
2.顯示聊天記錄
- - (NSInteger)tableView:(UITableView )tableView numberOfRowsInSection:(NSInteger)section {
- return self.messages.count;
- }
- - (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath {
- static NSString cellIndentifier = @"cell";
- UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
- if (cell==nil) {
- cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier];
- }
- //將聊天信息放到cell上
- //拿到一個聊天消息
- XMPPMessageArchiving_Message_CoreDataObject message = self.messages[indexPath.row];
- if (message.isOutgoing == YES) {
- cell.detailTextLabel.text = message.body;
- }
- }else{
- cell.textLabel.text = message.body;
- }
- return cell;
- }
成功后就可以聊天了:
演示圖
3.發送圖片等消息(重點)
發送視頻等其他文件也是一樣,xmpp中需要將圖片轉化成NSData,然后轉化成成base64的字符串進行傳輸,然后接收到之后再反轉化成圖片。
首先要訪問系統相冊。
遵循代理:
- @interface ChatViewController ()<XMPPStreamDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
從系統相冊選擇照片
發送圖片消息
這時候需要更改cellForRowAtIndexPath:方法,注意紅色部分。
發送圖片消息對應cell里也需要更改
我把圖片設置為cell的imageView,所以圖片顯示了在左邊,說明圖片消息發送是成功的,視頻等其他類型的消息,也是同樣的原理。
圖片消息演示
到這里,簡單的聊天功能就實現了,了解了基本原理和操作,我們自己還可以加入更多的自定義,從而優化得更好。這里僅僅講述了一些基本的方法,如果想了解更多,趕快自己動手實踐吧