admin管理员组

文章数量:1516870

XMPP增加删除好友

  在现阶段的通信服务中,各种标准都有,因此会出现无法实现相互连通,而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];//获取请求NSFetchRequestNSFetchRequest * 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:@"0"]) {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)

 

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

本文标签: XMPP增加删除好友