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];
    
    //获取请求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:@"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群:JRedu技术交流 (452379712)

 

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

 

转载于:https://www.cnblogs.com/wzjhoutai/p/7011000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值