XMPP增加移除朋友,xmpp增加好友

來源:互聯網
上載者:User

XMPP增加移除朋友,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群: (452379712)

 

作者:傑瑞教育
出處:http://www.cnblogs.com/jerehedu/ 
本文著作權歸煙台傑瑞教育科技有限公司和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文串連,否則保留追究法律責任的權利。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.