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

版权声明
本文由陈怀哲首发自简书:http://www.jianshu.com/users/9f2e536b78fd/latest_articles;
微信公众号:陈怀哲(chenhuaizhe2016);
无需授权即可转载,但请自觉保留以上版权声明。


好友和聊天流程图

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

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

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

好友

  1. 点击登录之后,验证成功就会跳到好友页面。这个时候需要显示你已经有的好友。
    那么在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()];
  2. 接收好友请求。
    将接收到好友请求的方法也封装在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; }}
  3. 添加好友,添加的好友必须是服务器上存在的用户,需要看对方是否同意。对方同意之后,刷新好友列表,显示出来,同时在服务器上也要添加,这里服务器上用的是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,所以图片显示了在左边,说明图片消息发送是成功的,视频等其他类型的消息,也是同样的原理。


图片消息演示

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



文/陈怀哲(简书作者)
原文链接:http://www.jianshu.com/p/8894a5a71b70
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
原文地址:https://www.cnblogs.com/Ghosgt/p/6007219.html