1. 程式人生 > >XMPP添加刪除好友

XMPP添加刪除好友

logs ise ngs rac commit ron tlab otto 定義

  在現階段的通信服務中。各種標準都有,因此會出現無法實現相互連通,而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群:技術分享 (452379712)

作者:傑瑞教育
出處:http://blog.csdn.net/jerehedu/
本文版權歸煙臺傑瑞教育科技有限公司和CSDN共同擁有,歡迎轉載,但未經作者允許必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。

XMPP添加刪除好友