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


//消息存档 @property(nonatomic,strong) XMPPMessageArchiving * messageArch; //消息存档存储模型 @property(nonatomic,strong) XMPPMessageArchivingCoreDataStorage * messageStore;
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];
-(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;
}
[[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;
}];
}
-(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://blog.csdn.net/jerehedu/article/details/47005997