日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

XMPP接受发送消息

發(fā)布時間:2025/7/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 XMPP接受发送消息 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  在現(xiàn)階段的通信服務(wù)中,各種標(biāo)準(zhǔn)都有,因此會出現(xiàn)無法實(shí)現(xiàn)相互連通,而XMPP(Extensible Message and presence Protocol)協(xié)議的出現(xiàn),實(shí)現(xiàn)了整個及時通信服務(wù)協(xié)議的互通。有了這個協(xié)議之后,使用任何一個組織或者個人提供的即使通信服務(wù),都能夠無障礙的與其他的及時通信服務(wù)的用戶進(jìn)行交流。例如google 公司2005年推出的Google talk就是一款基于XMPP協(xié)議的即時通信軟件。下面我們就談?wù)撘幌氯绾魏唵蔚氖褂肵MPP的接收和發(fā)送消息

1、在XMPPFramework.h中將需要用到的頭文件打開

?

2、在storyboard創(chuàng)建展示消息頁面

?

3、定義查詢結(jié)果存儲器并且進(jìn)行初始化

//消息存檔 @property(nonatomic,strong) XMPPMessageArchiving * messageArch;//消息存檔存儲模型 @property(nonatomic,strong) XMPPMessageArchivingCoreDataStorage * messageStore;

4、在上面storyboard創(chuàng)建的控制器中,添加如下代碼

AppDelegate * delgate=[UIApplication sharedApplication].delegate;//初始化頭像XMPPJID * fromJid=[XMPPJID jidWithString:self.fromJid];NSData * fromData=[delgate.vCardAvatarModule photoDataForJID:fromJid];self.fromImage=[[UIImage alloc] initWithData:fromData];NSString * userName= [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];XMPPJID * tojid=[XMPPJID jidWithString:userName];//設(shè)置圖片模型NSData * toData=[delgate.vCardAvatarModule photoDataForJID:tojid];self.meImage=[[UIImage alloc] initWithData:toData];if (self.fromImage==nil) {self.fromImage=[UIImage imageNamed:@"defalut"];}if (self.meImage==nil) {self.meImage=[UIImage imageNamed:@"defalut"];}[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKey:) name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKey:) name:UIKeyboardWillHideNotification object:nil];//初始化數(shù)據(jù)存儲NSString *user= [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];//初始化請求NSFetchRequest * request=[[NSFetchRequest alloc] initWithEntityName:@"XMPPMessageArchiving_Message_CoreDataObject"];request.predicate=[NSPredicate predicateWithFormat:@"bareJidStr=%@ and streamBareJidStr=%@",self.fromJid,user];//定義排序NSSortDescriptor * des=[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES];[request setSortDescriptors:@[des]];//獲取上下文NSManagedObjectContext *context=[delgate.messageStore mainThreadManagedObjectContext];//初始化結(jié)果存儲器fetch=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];//設(shè)置代理fetch.delegate=self;//開始查詢 [fetch performFetch:nil];

5、創(chuàng)實(shí)現(xiàn)tableView的代理方法

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{id <NSFetchedResultsSectionInfo> info=fetch.sections[section];NSLog(@"===%ld",info.numberOfObjects);return [info numberOfObjects];} - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ XMPPMessageArchiving_Message_CoreDataObject * obj=[fetch objectAtIndexPath:indexPath];JRChatTableViewCell * cell=nil;if (obj.isOutgoing) {cell=[tableView dequeueReusableCellWithIdentifier:@"cellto"];}else{cell=[tableView dequeueReusableCellWithIdentifier:@"cellfrom"];}//設(shè)置頭像cell.image.image=self.meImage;cell.selectionStyle=UITableViewCellSelectionStyleNone;[cell setText:obj.body WithFlag:obj.isOutgoing ];return cell;}

6、增加鍵盤控制

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKey:) name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKey:) name:UIKeyboardWillHideNotification object:nil]; - (void) showKey:(NSNotification * ) notify{CGFloat time=[notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] ;CGRect frame=[notify.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];[UIView animateWithDuration:time animations:^{self.keyView.transform=CGAffineTransformMakeTranslation(0, frame.size.height*-1);}]; }- (void) hideKey:(NSNotification * ) notify{CGFloat time=[notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] ;[UIView animateWithDuration:time animations:^{self.keyView.transform=CGAffineTransformIdentity;}]; }

7、發(fā)送消息

-(BOOL)textFieldShouldReturn:(UITextField *)textField{[textField resignFirstResponder];//發(fā)送消息//1 取出文本AppDelegate * delgate=[UIApplication sharedApplication].delegate;XMPPJID * jid=[XMPPJID jidWithString:self.fromJid];//初始化消息體XMPPMessage * message=[XMPPMessage messageWithType:@"chat" to:jid];[message addBody:self.ketf.text];//發(fā)送消息 [delgate.stream sendElement:message];//將消息置空self.ketf.text=nil;return YES; }

?

  想要了解更多內(nèi)容的小伙伴,可以點(diǎn)擊查看源碼,親自運(yùn)行測試。

  疑問咨詢或技術(shù)交流,請加入官方QQ群:?(452379712)

?

作者:杰瑞教育
出處:http://www.cnblogs.com/jerehedu/?
本文版權(quán)歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。

總結(jié)

以上是生活随笔為你收集整理的XMPP接受发送消息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。