當前位置:編程學習大全網 - 源碼下載 - ios怎樣在xmpp自帶的數據庫裏面插入數據

ios怎樣在xmpp自帶的數據庫裏面插入數據

點擊登錄之後,驗證成功就會跳到好友頁面。這個時候需要顯示妳已經有的好友。

那麽在tableViewCell中顯示好友姓名,需要數據源,數據源從服務器獲看妳是否有好友,檢索到妳的好友後把他顯示在列表上。

xmpp中管理好友的類是 XMPPRoster,並且使用coredata來儲存好友,達到數據持久化的效果。

那麽我們可以將獲取儲存好友的倉庫和xmppRoster對象的初始化封裝在XMPPManager中。

在.h文件中聲明:

//好友管理

@property(nonatomic,strong)XMPPRoster xmppRoster;

遵循代理:

@interface XMPPManager : NSObject<XMPPStreamDelegate,XMPPRosterDelegate>

在 .m文件中重寫init方法中:

//2.好友管理//獲得壹個存儲好友的CoreData倉庫,用來數據持久化 XMPPRosterCoreDataStorage rosterCoreDataStorage = [XMPPRosterCoreDataStorage sharedInstance];//初始化xmppRoster self.xmppRoster = [[XMPPRoster alloc]initWithRosterStorage:rosterCoreDataStorage dispatchQueue:dispatch_get_main_queue()];//激活 [self.xmppRoster activate:self.xmppStream];//設置代理 [self.xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];

接收好友請求。

將接收到好友請求的方法也封裝在XMPPManager中:

// 收到好友請求執行的方法-(void)xmppRoster:(XMPPRoster )sender didReceivePresenceSubscriptionRequest:(XMPPPresence )presence{ self.fromJid = presence.from; UIAlertView alert = [[UIAlertView alloc]initWithTitle:@"提示:有人添加妳" message:presence.from.user delegate:self cancelButtonTitle:@"拒絕" otherButtonTitles:@"OK", nil]; [alert show];}

-(void)alertView:(UIAlertView )alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ switch (buttonIndex) { case 0: [self.xmppRoster rejectPresenceSubscriptionRequestFrom:self.fromJid]; break; case 1: [self.xmppRoster acceptPresenceSubscriptionRequestFrom:self.fromJid andAddToRoster:YES]; break; default: break; }}

添加好友,添加的好友必須是服務器上存在的用戶,需要看對方是否同意。對方同意之後,刷新好友列表,顯示出來,同時在服務器上也要添加,這裏服務器上用的是coredata來存儲個人的好友信息。

好友頁面實現文件,遵循代理,數據源數組

在viewDidLoad中完成初始化數組,設置代理和添加好友按鈕

這裏簡化了添加好友,寫死了只能添加“張三”,如果需要添加更多,可以寫成借口

接下來是tableview數據源代理方法

tableview

這時候數組明顯是沒有jid對象的。獲取jid對象是在XMPPPRoster代理方法中實現的:

pragma mark xmppRoster 的代理方法

pragma mark 開始檢索好友列表的方法-(void)xmppRosterDidBeginPopulating:(XMPPRoster *)sender{ NSLog(@"開始檢索好友列表");}

pragma mark 正在檢索好友列表的方法-(void)xmppRoster:(XMPPRoster )sender didRecieveRosterItem:(DDXMLElement )item{ NSLog(@"每壹個好友都會走壹次這個方法");//獲得item的屬性裏的jid字符串,再通過它獲得jid對象 NSString jidStr = [[item attributeForName:@"jid"] stringValue]; XMPPJID jid = [XMPPJID jidWithString:jidStr];//是否已經添加 if ([self.rosterJids containsObject:jid]) { return; }//將好友添加到數組中去 [self.rosterJids addObject:jid];//添加完數據要更新UI(表視圖更新) NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.rosterJids.count-1 inSection:0]; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];}

pragma mark 好友列表檢索完畢的方法-(void)xmppRosterDidEndPopulating:(XMPPRoster )sender{ NSLog(@"好友列表檢索完畢");}

4. 刪除好友。列表刪除,數組刪除,服務器刪除。

pragma mark 刪除好友執行的方法-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath{ if (editingStyle==UITableViewCellEditingStyleDelete) { //找到要刪除的人 XMPPJID jid = self.rosterJids[indexPath.row];//從數組中刪除 [self.rosterJids removeObjectAtIndex:indexPath.row];//從Ui單元格刪除 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic ];//從服務器刪除 [[XMPPManager defaultManager].xmppRoster removeUser:jid]; }}

5.進入聊天頁面

  • 上一篇:PddS單壹源代碼
  • 下一篇:紅警怎麽玩新手教程
  • copyright 2024編程學習大全網