XMPP协议获取花名册(好友列表)
生活随笔
收集整理的這篇文章主要介紹了
XMPP协议获取花名册(好友列表)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先導(dǎo)入花名冊模塊,理由同上篇文章,這里不再具體說明
// 花名冊模塊 #import "XMPPRoster.h" #import "XMPPRosterCoreDataStorage.h"
//設(shè)置花名冊模塊self.rosterStorage=[[XMPPRosterCoreDataStorage alloc]init];self.roster=[[XMPPRoster alloc]initWithRosterStorage:self.rosterStorage];[self.roster activate:self.stream];
花名冊模塊也是先從服務(wù)器獲取在存到本地數(shù)據(jù)庫,再從本地數(shù)據(jù)庫獲取利用CoreData花名冊存在XMPPUserCoreDataStorageObject表中,CordData有個很強大的類是
NSFetchedResultsController,他可以監(jiān)聽數(shù)據(jù)值的變化,如果設(shè)置代理了一但變化就會通知代理,這時候當(dāng)一個用戶的在線狀態(tài)發(fā)生變化時刷新表格即可,免得用kvo監(jiān)聽屬性比較麻煩<當(dāng)切換登錄用戶時會把數(shù)據(jù)庫里的好友數(shù)據(jù)刪除,在把新的當(dāng)前用戶的好友數(shù)據(jù)加到數(shù)據(jù)庫>
XMPPTool *tool=[XMPPTool sharedXMPPTool];NSManagedObjectContext *context=tool.rosterStorage.mainThreadManagedObjectContext;NSFetchRequest *request=[[NSFetchRequest alloc]initWithEntityName:@"XMPPUserCoreDataStorageObject"];//對結(jié)果進(jìn)行排序NSSortDescriptor *sort=[NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES];request.sortDescriptors=@[sort];//設(shè)置謂詞過濾NSPredicate *pre=[NSPredicate predicateWithFormat:@"subscription!=%@",@"none"];request.predicate=pre;//此處是設(shè)置過濾,none為加好友請求沒有同意的好友self.resultController=[[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];//設(shè)置代理self.resultController.delegate=self;NSError *error=nil;//執(zhí)行[self.resultController performFetch:&error];if (error) {LSLog(@"出錯:%@",error);}取出表中每一行數(shù)據(jù),即一個好友用戶信息,user.photo為nil,所以每個好友頭像都得自己利用頭像模塊獲取 XMPPUserCoreDataStorageObject *user=self.resultController.fetchedObjects[indexPath.row];if (user.photo) {cell.imageView.image=user.photo;}else {XMPPTool *tool=[XMPPTool sharedXMPPTool];cell.imageView.image= [UIImage imageWithData:[tool.avatar photoDataForJID:user.jid]];}cell.textLabel.text=user.nickname;switch (user.sectionNum.intValue) {case 0:cell.detailTextLabel.text=@"在線";break;case 1:cell.detailTextLabel.text=@"離開";break;case 2:cell.detailTextLabel.text=@"離線";break;default:cell.detailTextLabel.text=@"未知";break;}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {[self.tableView reloadData]; }
刪除好友,利用花名冊模塊刪除
XMPPTool *tool=[XMPPTool sharedXMPPTool];XMPPUserCoreDataStorageObject *user=self.resultController.fetchedObjects[indexPath.row];[tool.roster removeUser:user.jid];加好友,首先利用花名測存儲模塊判斷是否存在當(dāng)前要添加用戶 if (self.contactTextField.text==nil) {[KVNProgress showErrorWithStatus:@"請輸入用戶名"];return;}if ([self.contactTextField.text isEqualToString:[LSAccount sharedAccount].loginName]) {[KVNProgress showErrorWithStatus:@"不能添加自己"];return;}XMPPTool *tool=[XMPPTool sharedXMPPTool];NSString *user=self.contactTextField.text;XMPPJID *userJid=[XMPPJID jidWithUser:user domain:[LSAccount sharedAccount].domain resource:@"iPhone6s"];if ([tool.rosterStorage userExistsWithJID:userJid xmppStream:tool.stream]){[KVNProgress showErrorWithStatus:@"此好友已經(jīng)存在"];}else {//在XMPP里訂閱即是添加[tool.roster subscribePresenceToUser:userJid];}/*添加好友在現(xiàn)有openfire存在的問題1.添加不存在的好友,通訊錄里面也現(xiàn)示了好友解決辦法1. 服務(wù)器可以攔截好友添加的請求,如當(dāng)前數(shù)據(jù)庫沒有好友,不要返回信息<presence type="subscribe" to="werqqrwe@127.0.0.1"><x xmlns="vcard-temp:x:update"><photo>b5448c463bc4ea8dae9e0fe65179e1d827c740d0</photo></x></presence>解決辦法2.過濾數(shù)據(jù)庫的Subscription字段查詢請求none 對方?jīng)]有同意添加好友to 發(fā)給對方的請求from 別人發(fā)來的請求both 雙方互為好友*/
總結(jié)
以上是生活随笔為你收集整理的XMPP协议获取花名册(好友列表)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 日本k线图技术
- 下一篇: 计算机在模具设计中的应用课程设计,计算机