在現階段的通信服務中,各種標準都有,因此會出現無法實現相互連通,而XMPP(Extensible Message and PResence Protocol)協議的出現,實現了整個及時通信服務協議的互通。有了這個協議之后,使用任何一個組織或者個人提供的即使通信服務,都能夠無障礙的與其他的及時通信服務的用戶進行交流。例如google 公司2005年推出的Google talk就是一款基于XMPP協議的即時通信軟件。下面我們就談論一下如何簡單的使用XMPP的好友添加
創建和增加的時候應該注意兩者之間的連線:
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];
#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;}
-(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]; }}
- (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)
新聞熱點
疑難解答