XMPP接受发送消息

时间:2022-10-22 20:16:22

  在现阶段的通信服务中,各种标准都有,因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现,实现了整个及时通信服务协议的互通。有了这个协议之后,使用任何一个组织或者个人提供的即使通信服务,都能够无障碍的与其他的及时通信服务的用户进行交流。例如google 公司2005年推出的Google talk就是一款基于XMPP协议的即时通信软件。下面我们就谈论一下如何简单的使用XMPP的接收和发送消息

1、在XMPPFramework.h中将需要用到的头文件打开

XMPP接受发送消息

2、在storyboard创建展示消息页面

XMPP接受发送消息

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群:XMPP接受发送消息 (452379712)

作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。