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