zl程序教程

您现在的位置是:首页 >  APP

当前栏目

iOS中XMPP简单聊天实现 好友和聊天

2023-04-18 15:00:16 时间

好友和聊天流程图

在看这篇文章之前,你需要配置好服务器,以及完成注册和登录的基本功能,才能继续好友和聊天的操作。

下面两篇文章是环境配置和注册、登录功能的详细介绍:
XMPP的mysql和openfire环境配置
iOS中XMPP简单聊天实现 注册和登录

另外必须了解一些CoreData相关知识

好友

  1. 点击登录之后,验证成功就会跳到好友页面。这个时候需要显示你已经有的好友。
    那么在tableViewCell中显示好友姓名,需要数据源,数据源从服务器获看你是否有好友,检索到你的好友后把他显示在列表上。
    xmpp中管理好友的类是 XMPPRoster,并且使用coredata来储存好友,达到数据持久化的效果。
    那么我们可以将获取储存好友的仓库和xmppRoster对象的初始化封装在XMPPManager中。
    在.h文件中声明:

    1. //好友管理 
    2. @property(nonatomic,strong)XMPPRoster xmppRoster; 

    遵循代理:

    1. @interface XMPPManager : NSObject<XMPPStreamDelegate,XMPPRosterDelegate> 

    在 .m文件中重写init方法中:
    1. //2.好友管理//获得一个存储好友的CoreData仓库,用来数据持久化 
    2.     XMPPRosterCoreDataStorage rosterCoreDataStorage = [XMPPRosterCoreDataStorage sharedInstance]; 
    3. //初始化xmppRoster 
    4.     self.xmppRoster = [[XMPPRoster alloc]initWithRosterStorage:rosterCoreDataStorage dispatchQueue:dispatch_get_main_queue()]; 
    5. //激活 
    6.    [self.xmppRoster activate:self.xmppStream]; 
    7. //设置代理 
    8.   [self.xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()]; 
  2. 接收好友请求。
    将接收到好友请求的方法也封装在XMPPManager中:

    1. // 收到好友请求执行的方法 
    2. -(void)xmppRoster:(XMPPRoster )sender didReceivePresenceSubscriptionRequest:(XMPPPresence )presence{ 
    3.  self.fromJid = presence.from; 
    4.  UIAlertView alert = [[UIAlertView alloc]initWithTitle:@"提示:有人添加你" message:presence.from.user  delegate:self cancelButtonTitle:@"拒绝" otherButtonTitles:@"OK", nil]; 
    5.  [alert show]; 
    1. -(void)alertView:(UIAlertView )alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    2.   switch (buttonIndex) { 
    3.    case 0
    4.    [self.xmppRoster rejectPresenceSubscriptionRequestFrom:self.fromJid]; 
    5.   break
    6.    case 1
    7.    [self.xmppRoster acceptPresenceSubscriptionRequestFrom:self.fromJid andAddToRoster:YES]; 
    8.    break
    9.    default
    10.    break
    11.  } 
  3. 添加好友,添加的好友必须是服务器上存在的用户,需要看对方是否同意。对方同意之后,刷新好友列表,显示出来,同时在服务器上也要添加,这里服务器上用的是coredata来存储个人的好友信息。

好友页面实现文件,遵循代理,数据源数组

在viewDidLoad中完成初始化数组,设置代理和添加好友按钮

这里简化了添加好友,写死了只能添加“张三”,如果需要添加更多,可以写成借口

接下来是tableview数据源代理方法

tableview

这时候数组明显是没有jid对象的。获取jid对象是在XMPPPRoster代理方法中实现的:

pragma mark xmppRoster 的代理方法

  1. pragma mark 开始检索好友列表的方法 
  2. -(void)xmppRosterDidBeginPopulating:(XMPPRoster *)sender{ 
  3.     NSLog(@"开始检索好友列表"); 
  1. pragma mark 正在检索好友列表的方法 
  2. -(void)xmppRoster:(XMPPRoster )sender didRecieveRosterItem:(DDXMLElement )item{ 
  3.     NSLog(@"每一个好友都会走一次这个方法"); 
  4. //获得item的属性里的jid字符串,再通过它获得jid对象 
  5.     NSString jidStr = [[item attributeForName:@"jid"] stringValue]; 
  6.     XMPPJID jid = [XMPPJID jidWithString:jidStr]; 
  7. //是否已经添加 
  8.     if ([self.rosterJids containsObject:jid]) { 
  9.         return
  10.     } 
  11. //将好友添加到数组中去 
  12.     [self.rosterJids addObject:jid]; 
  13. //添加完数据要更新UI(表视图更新) 
  14.     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.rosterJids.count-1 inSection:0]; 
  15.     [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
  1. pragma mark 好友列表检索完毕的方法 
  2. -(void)xmppRosterDidEndPopulating:(XMPPRoster )sender{ 
  3.     NSLog(@"好友列表检索完毕"); 

4. 删除好友。列表删除,数组删除,服务器删除。

  1. pragma mark 删除好友执行的方法 
  2. -(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath{ 
  3.     if (editingStyle==UITableViewCellEditingStyleDelete) { 
  4.  //找到要删除的人 
  5.         XMPPJID jid = self.rosterJids[indexPath.row]; 
  6. //从数组中删除 
  7.         [self.rosterJids removeObjectAtIndex:indexPath.row]; 
  8. //从Ui单元格删除 
  9.         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic 
  10.          ]; 
  11. //从服务器删除 
  12.         [[XMPPManager defaultManager].xmppRoster removeUser:jid]; 
  13.     } 

5.进入聊天页面

点击进入聊天页面的方法

聊天页面接受jid值的属性

聊天

1.发送普通文本消息
同样在XMPPManager中进行封装;

  1. //聊天信息归档 
  2. @property(nonatomic,strong)XMPPMessageArchiving xmppMessageArchiving; 
  3. //信息归档的上下文 
  4. @property(nonatomic,strong)NSManagedObjectContext messageArchivingContext; 

在init初始化时:

  1. //3.保存聊天记录 
  2.   //初始化一个仓库 
  3.         XMPPMessageArchivingCoreDataStorage *messageStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance]; 
  4.   //创建一个消息归档对象 
  5.         self.xmppMessageArchiving = [[XMPPMessageArchiving alloc]initWithMessageArchivingStorage:messageStorage dispatchQueue:dispatch_get_main_queue()]; 
  6.   //激活 
  7.         [self.xmppMessageArchiving activate:self.xmppStream]; 
  8.   //上下文 
  9.         self.messageArchivingContext = messageStorage.mainThreadManagedObjectContext; 

在聊天页面的viewDidload中:

发送普通消息:

  1. -(void)doSend{ 
  2.     //创建一个消息对象,并且指明接收者 
  3.     XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:self.chatToJid]; 
  4.     //设置消息内容 
  5.     [message addBody:@"呵呵呵呵呵呵呵呵呵呵"]; 
  6.     //发送消息 
  7.     [[XMPPManager defaultManager].xmppStream sendElement:message]; 
  8.     //发送成功或者失败,有两种对应的代理方法 

消息发送是否成功,会走下面的代理方法:

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.显示聊天记录

  1. - (NSInteger)tableView:(UITableView )tableView numberOfRowsInSection:(NSInteger)section { 
  2.     return self.messages.count; 
  1. - (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath { 
  2.     static NSString cellIndentifier = @"cell"
  3.     UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier]; 
  4.     if (cell==nil) { 
  5.      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier]; 
  6.     } 
  7.    //将聊天信息放到cell上 
  8.     //拿到一个聊天消息 
  9.     XMPPMessageArchiving_Message_CoreDataObject message = self.messages[indexPath.row]; 
  10.     if (message.isOutgoing == YES) { 
  11.      cell.detailTextLabel.text = message.body; 
  12.      } 
  13.     }else
  14.      cell.textLabel.text = message.body; 
  15.     } 
  16.     return cell; 

成功后就可以聊天了:

演示图

 3.发送图片等消息(重点)
发送视频等其他文件也是一样,xmpp中需要将图片转化成NSData,然后转化成成base64的字符串进行传输,然后接收到之后再反转化成图片。
首先要访问系统相册。
遵循代理:

  1. @interface ChatViewController ()<XMPPStreamDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> 

从系统相册选择照片

发送图片消息

这时候需要更改cellForRowAtIndexPath:方法,注意红色部分。

发送图片消息对应cell里也需要更改

我把图片设置为cell的imageView,所以图片显示了在左边,说明图片消息发送是成功的,视频等其他类型的消息,也是同样的原理。

图片消息演示

到这里,简单的聊天功能就实现了,了解了基本原理和操作,我们自己还可以加入更多的自定义,从而优化得更好。这里仅仅讲述了一些基本的方法,如果想了解更多,赶快自己动手实践吧