在现阶段的通信服务中,各种标准都有,因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现,实现了整个及时通信服务协议的互通。有了这个协议之后,使用任何一个组织或者个人提供的即使通信服务,都能够无障碍的与其他的及时通信服务的用户进行交流。例如google 公司2005年推出的Google talk就是一款基于XMPP协议的即时通信软件。下面我们就谈论一下如何简单的使用XMPP的好友添加

1、在XMPPFramework.h中将需要用到的头文件打开

2、在storyboard创建好友列表界面和增加好友界面

创建和增加的时候应该注意两者之间的连线:

3、定义查询结果存储器并且进行初始化

NSFetchedResultsController * fetch;

    AppDelegate * delegate=[UIApplication sharedApplication].delegate;

    //获取上下文
NSManagedObjectContext * context=[delegate.rosterStorage mainThreadManagedObjectContext]; //获取请求NSFetchRequest
NSFetchRequest * request=[NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"]; //增加排序字段
NSSortDescriptor * des=[NSSortDescriptor sortDescriptorWithKey:@"sectionNum" ascending:YES];
[request setSortDescriptors:@[des]]; //对fetch进行初始化
fetch=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:@"sectionNum" cacheName:nil]; //设置代理
[fetch setDelegate:self]; //开始查询
[fetch performFetch:nil];

4、创实现tableView的代理方法

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return fetch.sections.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { id<NSFetchedResultsSectionInfo> sections=fetch.sections[section];
return [sections numberOfObjects] ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tabble" ]; XMPPUserCoreDataStorageObject * user=[fetch objectAtIndexPath:indexPath];
cell.textLabel.text=user.displayName; return cell;
} //返回分组数据信息,根据字段type来控制当前的状态
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ id<NSFetchedResultsSectionInfo> info= [[fetch sections] objectAtIndex:section]; NSString * type=nil; if ([info.name isEqualToString:@""]) {
type=@"在线";
}else {
type=@"离线";
} return type; }

5、删除好友

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;
} //通过花名册对象对当前的好友进行删除操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle==UITableViewCellEditingStyleDelete) {
AppDelegate * delegate=[UIApplication sharedApplication] .delegate; XMPPUserCoreDataStorageObject * user=[fetch objectAtIndexPath:indexPath]; [delegate.roster removeUser:user.jid]; }
}

6、增加好友

- (IBAction)add {
//获取好友名称
NSString * name=self.friends.text;
//获取好友名称
AppDelegate * delegate=[UIApplication sharedApplication].delegate;
XMPPJID * jid=[XMPPJID jidWithString:name];
if ([delegate.rosterStorage userExistsWithJID:jid xmppStream:delegate.stream]) {
NSLog(@"好友已经存在了");
}else{
//增加好友
[delegate.roster subscribePresenceToUser:jid]; }
}

  想要了解更多内容的小伙伴,可以点击查看源码,亲自运行测试。

  疑问咨询或技术交流,请加入官方QQ群: (452379712)

作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

XMPP增加删除好友的更多相关文章

  1. XMPP添加删除好友

    在现阶段的通信服务中.各种标准都有,因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现,实现了整个及时通信服务协议的互 ...

  2. XMPP and SIP

    过去一年多,一直关注这方面的技术和发展,这里有一个简单的介绍,我觉得比较简洁明了.我做了一点翻译,还有我的一些评估.       SIP vs XMPP (Jabber) SIP and XMPP a ...

  3. SIP SDP RTSP RTP RTCP webrtc

    rfc1889  rfc2326  rfc3261  rfc3550  rfc3856  rfc6120. SIP SDP RTSP  RTP RTCP,就像他们出现的顺序一样,他们在实际应用中的启用 ...

  4. Smack类库详细介绍

    原文地址:http://blog.csdn.net/xunshu/archive/2008/03/27/2223817.aspx Smack是一个为使用XMPP服务器聊天和发送即时消息交流而提供的库. ...

  5. SIP学习笔记 -- RFC 3261

    1.SDP (rfc 4566)    1)用于交换参数    2)内容分三部分Session description, Time description and Media description ...

  6. 在XMPP的JAVA开源实现Openfire中,增加LBS 附近的人功能

    1. XMPP协议 与 Openfire XMPP协议是IM领域的标准协议了,具体可参考  http://xmpp.org   及RFC6120,RFC6121,RFC6122等相关文档. http: ...

  7. xmpp获取好友信息和添加删除好友(4)

    原始地址: XMPPFrameWork IOS 开发(五)获取好友信息和添加删除好友 好友列表和好友名片 [_xmppRoster fetchRoster];//获取好友列表 //获取到一个好友节点 ...

  8. XMPP即时通讯

    XMPP:XMPP是基于XML的点对点通讯协议,The Extensible Messaging and Presence Protocol(可扩展通讯和表示协议). XMPP可用于服务类实时通讯,表 ...

  9. UI进阶 即时通讯之XMPP好友列表、添加好友、获取会话内容、简单聊天

    这篇博客的代码是直接在上篇博客的基础上增加的,先给出部分代码,最后会给出能实现简单功能的完整代码. UI进阶 即时通讯之XMPP登录.注册 1.好友列表 初始化好友花名册 #pragma mark - ...

随机推荐

  1. POJ 2503 单词映射(map)

    Sample Input dog ogdaycat atcaypig igpayfroot ootfrayloops oopslay atcayittenkayoopslaySample Output ...

  2. 快速构建ceph可视化监控系统

    https://my.oschina.net/colben/blog/1844602 https://my.oschina.net/u/3626804/blog/1859613

  3. 记一次Oracle分区表错误:ORA-14400: 插入的分区关键字未映射到任何分区

    https://blog.csdn.net/xdyzgjy/article/details/42238735

  4. PHPexcel的用法

    由于经常要统计学生的考试成绩,就研究了下PHPexcel这个插件 顺便说一下,读取方法只针对xls文件. 如果报错,可以先生存一个xls文件,把需要读取的xls内容复制进去. <?php //读 ...

  5. linux 中的./configuration --prefix=安装路径 的用法(指定源码安装方式的安装路基)

    源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install). Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./con ...

  6. 记录一个mysql的case when用法

    SELECT wle.*, CASE WHEN '2017-08-10 14:00:00' > wle.et THEN '回看' WHEN wle.st >= '2017-08-10 14 ...

  7. [Web安全] XXE漏洞攻防学习(中)

    0x00.XXE漏洞攻击实例 攻击思路: 1. 引用外部实体远程文件读取 2. Blind XXE 3. Dos 0x01.外部实体引用,有回显 实验操作平台:bWAPP平台上的XXE题目 题目: 进 ...

  8. Ubuntu python Compression requires the (missing) zlib module

    描述: 在Ubuntu中安装setuptools时出现   Compression requires the (missing) zlib module 解决方法步骤: ①Ubuntu下安装zlib: ...

  9. 开始使用Reflection

    用于 reflection 的类,如 Method,可以在 java.lang.relfect 包中找到.使用这些类的时候必须要遵循三个步骤:第一步是获得你想操作的类的 java.lang.Class ...

  10. 你了解ECMAScript吗?

    一.基本概念ECMA,European Computer Manufacturers Association,欧洲计算机制造协会. TC39,Technical Committee 39,ECMA的第 ...