zoukankan      html  css  js  c++  java
  • 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共同拥有,欢迎转载,但未经作者允许必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

     
  • 相关阅读:
    17.1.2.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication
    17.1.2 Replication Formats
    Setting the Master Configuration on the Slave
    17.1.1.9 Introducing Additional Slaves to an Existing Replication Environment
    17.1.1.8 Setting Up Replication with Existing Data
    17.1.1.7 Setting Up Replication with New Master and Slaves
    17.1.1.6 Creating a Data Snapshot Using Raw Data Files
    列出display的值,并说明它们的作用
    CSS设置DIV居中
    CSS选择符有哪些?哪些属性可以继承?优先级算法如何计算?
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7011000.html
Copyright © 2011-2022 走看看