在現(xiàn)階段的通信服務(wù)中,各種標(biāo)準(zhǔn)都有,因此會(huì)出現(xiàn)無法實(shí)現(xiàn)相互連通,而XMPP(Extensible Message and PResence Protocol)協(xié)議的出現(xiàn),實(shí)現(xiàn)了整個(gè)及時(shí)通信服務(wù)協(xié)議的互通。有了這個(gè)協(xié)議之后,使用任何一個(gè)組織或者個(gè)人提供的即使通信服務(wù),都能夠無障礙的與其他的及時(shí)通信服務(wù)的用戶進(jìn)行交流。例如google 公司2005年推出的Google talk就是一款基于XMPP協(xié)議的即時(shí)通信軟件。下面我們就談?wù)撘幌氯绾魏唵蔚氖褂?span style="mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-fareast; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋體;" lang="EN-US">XMPP的好友添加
創(chuàng)建和增加的時(shí)候應(yīng)該注意兩者之間的連線:
NSFetchedResultsController * fetch; AppDelegate * delegate=[UIapplication sharedApplication].delegate; //獲取上下文 NSManagedObjectContext * context=[delegate.rosterStorage mainThreadManagedObjectContext]; //獲取請(qǐng)求NSFetchRequest NSFetchRequest * request=[NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"]; //增加排序字段 NSSortDescriptor * des=[NSSortDescriptor sortDescriptorWithKey:@"sectionNum" ascending:YES]; [request setSortDescriptors:@[des]]; //對(duì)fetch進(jìn)行初始化 fetch=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:@"sectionNum" cacheName:nil]; //設(shè)置代理 [fetch setDelegate:self]; //開始查詢 [fetch performFetch:nil];
#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;}//返回分組數(shù)據(jù)信息,根據(jù)字段type來控制當(dāng)前的狀態(tài)-(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;}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES;}//通過花名冊(cè)對(duì)象對(duì)當(dāng)前的好友進(jìn)行刪除操作-(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]; }}
- (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(@"好友已經(jīng)存在了"); }else{ //增加好友 [delegate.roster subscribePresenceToUser:jid]; }}
想要了解更多內(nèi)容的小伙伴,可以點(diǎn)擊查看源碼,親自運(yùn)行測(cè)試。
疑問咨詢或技術(shù)交流,請(qǐng)加入官方QQ群: (452379712)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注