[置顶] XMPPFrameWork IOS 开发(四)消息和好友上下线
2014-05-06 14:06
435 查看
原始地址:XMPPFrameWork IOS 开发(四)
消息
[cpp] view
plaincopyprint?
//收到消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
// NSLog(@"message = %@", message);
//消息的内容
NSString *msg = [[message elementForName:@"body"] stringValue];
//消息发送者
NSString *from = [[message attributeForName:@"from"] stringValue];
/****在此处****/
//通知聊天页面有新消息,需要处理
}
发送消息
[cpp] view
plaincopyprint?
//发送消息的xml格式
<message from='发送者账号'
to='接收者账号'
type='chat'>
<body>HELLO WORLD </body>
</message>
//代码组装
[cpp] view
plaincopyprint?
NSString *message = @"HELLO WORLD";
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message];
//生成XML消息文档
NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
//消息类型
[mes addAttributeWithName:@"type" stringValue:@"chat"];
//发送给谁
[mes addAttributeWithName:@"to" stringValue:@"接受者账号"];
//由谁发送
[mes addAttributeWithName:@"from" stringValue:@"发送者账号"];
//组合
[mes addChild:body];
//发送消息
[[self xmppStream] sendElement:mes];
好友上下线通知
[cpp] view
plaincopyprint?
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
//取得好友状态
NSString *presenceType = [presence type]; //online/offline
//当前用户
NSString *userId = [[sender myJID] user];
//在线用户
NSString *presenceFromUser = [[presence from] user];
/*
//如果不是自己,如果涉及多段登录,此处最好加上else,如果是自己离线的话,调用上线协议
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence];
*/
if (![presenceFromUser isEqualToString:userId])
{
//用户在线
if ([presenceType isEqualToString:@"available"])
{
//列表和数据库都要相应改变
}else if ([presenceType isEqualToString:@"unavailable"])//用户不在线
{
//列表和数据库都要相应改变
}
}
}
消息
[cpp] view
plaincopyprint?
//收到消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
// NSLog(@"message = %@", message);
//消息的内容
NSString *msg = [[message elementForName:@"body"] stringValue];
//消息发送者
NSString *from = [[message attributeForName:@"from"] stringValue];
/****在此处****/
//通知聊天页面有新消息,需要处理
}
发送消息
[cpp] view
plaincopyprint?
//发送消息的xml格式
<message from='发送者账号'
to='接收者账号'
type='chat'>
<body>HELLO WORLD </body>
</message>
//代码组装
[cpp] view
plaincopyprint?
NSString *message = @"HELLO WORLD";
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message];
//生成XML消息文档
NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
//消息类型
[mes addAttributeWithName:@"type" stringValue:@"chat"];
//发送给谁
[mes addAttributeWithName:@"to" stringValue:@"接受者账号"];
//由谁发送
[mes addAttributeWithName:@"from" stringValue:@"发送者账号"];
//组合
[mes addChild:body];
//发送消息
[[self xmppStream] sendElement:mes];
好友上下线通知
[cpp] view
plaincopyprint?
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
//取得好友状态
NSString *presenceType = [presence type]; //online/offline
//当前用户
NSString *userId = [[sender myJID] user];
//在线用户
NSString *presenceFromUser = [[presence from] user];
/*
//如果不是自己,如果涉及多段登录,此处最好加上else,如果是自己离线的话,调用上线协议
XMPPPresence *presence = [XMPPPresence presence];
[[self xmppStream] sendElement:presence];
*/
if (![presenceFromUser isEqualToString:userId])
{
//用户在线
if ([presenceType isEqualToString:@"available"])
{
//列表和数据库都要相应改变
}else if ([presenceType isEqualToString:@"unavailable"])//用户不在线
{
//列表和数据库都要相应改变
}
}
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- [原创]java局域网聊天系统
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- javascript Discuz代码中的msn聊天小功能
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 探讨Android与iOS,我们将何去何从?
- IOS 身份证校验详细介绍及示例代码
- iOS NSDate中关于夏令时的坑
- iOS内存错误EXC_BAD_ACCESS的解决方法
- 从 Auto Layout 的布局算法谈性能
- 浅谈 MVC、MVP 和 MVVM 架构模式
- 深入解析 ObjC 中方法的结构
- 你真的了解 load 方法么?
- 从源代码看 ObjC 中消息的发送
- vm安装mac os x 10.7 lion
- objective-c(一)
- objective-c(三)