XMPP接受发送消息
在现阶段的通信服务中,各种标准都有,因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现,实现了整个及时通信服务协议的互通。有了这个协议之后,使用任何一个组织或者个人提供的即使通信服务,都能够无障碍的与其他的及时通信服务的用户进行交流。例如google 公司2005年推出的Google talk就是一款基于XMPP协议的即时通信软件。下面我们就谈论一下如何简单的使用XMPP的接收和发送消息
1、在XMPPFramework.h中将需要用到的头文件打开
2、在storyboard创建展示消息页面
3、定义查询结果存储器并且进行初始化
//消息存档
@property(nonatomic,strong) XMPPMessageArchiving * messageArch; //消息存档存储模型
@property(nonatomic,strong) XMPPMessageArchivingCoreDataStorage * messageStore;
4、在上面storyboard创建的控制器中,添加如下代码
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]; //设置图片模型
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]; //初始化数据存储
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]; //初始化结果存储器
fetch=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; //设置代理
fetch.delegate=self; //开始查询
[fetch performFetch:nil];
5、创实现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"];
} //设置头像
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(, frame.size.height*-);
}];
} - (void) hideKey:(NSNotification * ) notify{
CGFloat time=[notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] ;
[UIView animateWithDuration:time animations:^{
self.keyView.transform=CGAffineTransformIdentity;
}];
}
7、发送消息
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder]; //发送消息 //1 取出文本
AppDelegate * delgate=[UIApplication sharedApplication].delegate; XMPPJID * jid=[XMPPJID jidWithString:self.fromJid]; //初始化消息体
XMPPMessage * message=[XMPPMessage messageWithType:@"chat" to:jid];
[message addBody:self.ketf.text]; //发送消息
[delgate.stream sendElement:message]; //将消息置空
self.ketf.text=nil;
return YES;
}
想要了解更多内容的小伙伴,可以点击查看源码,亲自运行测试。
疑问咨询或技术交流,请加入官方QQ群: (452379712)
出处:http://www.cnblogs.com/jerehedu/
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
XMPP接受发送消息的更多相关文章
- C# 微信公众平台开发(3)-- 接受/发送消息
1.接收事件 关注/取消关注事件: 用户在关注与取消关注公众号时,微信会把这个事件推送到开发者填写的URL. 通过微信返回的XML,我们通过代码 就可以作相应的处理: 先获取返回的信息: if (Ht ...
- 关于xmpp协议发送消息,登录认证SSL报错的问题
Q:错误描述如下 Traceback(most recent call last): File"/tails-share/features/scripts/otr-bot.py", ...
- Xmpp学习之Smack发送消息JID变乱码
Xmpp学习之Smack发送消息JID变乱码 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/6947723 ...
- Handler发送消息
Handler发送消息小结 字数283 阅读210 评论0 喜欢1 obtainMessage()得到一个Message对象. 创建一个Message然后发送是这么写的: Message msg = ...
- 如何给对话框中的控件发送消息呢?Windows消息分类
以博文CTabCtrl中介绍的那样,给Tab添加子对话框来显示Tab内容.那么如果这个子对话框中含有个CTreeCtrl控件,有个Button控件,我想要模拟给这两个控件发送消息,该怎么办呢?直接把给 ...
- XMPP协议之消息回执解决方案
苦恼中寻找方法 在开始做即时通信时就知道了消息回执这个概念,目的是解决通讯消息因为各种原因未送达对方而提供的一种保障机制.产生这个问题的原因主要是网络不稳定.服务器或者客户端一些异常导致没有接收到消息 ...
- Golang之发送消息至kafka
windows下安装zookeeper 1.安装JAVA-JDK,从oracle下载最新的SDK安装(我用的是1.8的) 2.安装zookeeper3.3.6,下载地址:http://apache.f ...
- Openfire 单人聊天和多人聊天(发送消息、接收消息)
Openfire 单人聊天和多人聊天(发送消息.接收消息) 一.单人聊天 1)发送消息: 首先要获取一个聊天窗口,getConnection()为获取连接connection的方法,调用getFrie ...
- WebSocket.之.基础入门-前端发送消息
WebSocket.之.基础入门-前端发送消息 在<WebSocket.之.基础入门-建立连接>的代码基础之上,进行添加代码.代码只改动了:TestSocket.java 和 index. ...
随机推荐
- SeaJS入门教程系列之使用SeaJS(二)
SeaJS入门教程系列之使用SeaJS(二) 作者: 字体:[增加 减小] 类型:转载 时间:2014-03-03我要评论 这篇文章主要介绍了SeaJS入门教程系列之使用SeaJS,着重介绍了SeaJ ...
- 使用siege执行压力测试
没有安装siege? 可参考我的另一篇博客 使用siege执行压力测试笔记 场景分析 使用siege对https://www.baidu.com/进行加压. 要求 模拟20个用户同时访问 一共跑3个循 ...
- 主机可以ping通虚拟机,但是虚拟机ping不通主机的方法(转)
https://blog.csdn.net/hskw444273663/article/details/81301470
- 【APUE | 10】函数signal
函数signal 函数signal介绍 typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t ...
- #9 //[SDOI2017]新生舞会
题解: 分数规划+费用流 常数巨大开o2加inline加register还是不行 我也不知道为什么 代码: #include <bits/stdc++.h> using namespace ...
- POI搜索简介
用户输入——用户输出-----------------------------------------------------------而POI搜索引擎,需要做的就是拿到输入条件,给出用户比较满意的 ...
- php特别值
if(!isset($lichi)){ echo'未定义'; if(empty($lichi)){ echo '未定义的显示为空的';//最终会走到这来 } } 手册类型比较表 empty为真$x = ...
- 文件压缩 ZipOutputStream类
先举一个压缩单个文件的例子吧: [例子1] import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...
- Python3绘图库Matplotlib(02)
控制颜色 Color Color Name b blue c cyan g green k black m magenta r red w white y yellow plt.plot(x1, y1 ...
- Java并发程序设计(十一)设计模式与并发之生产者-消费者模式
设计模式与并发之生产者-消费者模式 生产者-消费者模式是一个经典的多线程设计模式.它为多线程间的协作提供了良好的解决方案.在生产者-消费者模式中,通常由两类线程,即若干个生产者线程和若干个消费者线程. ...