XMPPFramework是一个OS X/iOS平台的开源项目,使用Objective-C实现了XMPP协议(RFC-3920),同时还提供了用于读写XML的工具,大大简化了基于XMPP的通信应用的开发。
1. 登录和好友上下线
1.1XMPP中常用对象们
XMPPStream:xmpp基础服务类 XMPPRoster:好友列表类 XMPPRosterCoreDataStorage:好友列表(用户账号)在core data中的操作类 XMPPvCardCoreDataStorage:好友名片(昵称,签名,性别,年龄等信息)在core data中的操作类 XMPPvCardTemp:好友名片实体类,从数据库里取出来的都是它 xmppvCardAvatarModule:好友头像 XMPPReconnect:如果失去连接,自动重连 XMPPRoom:提供多用户聊天支持 XMPPPubSub:发布订阅1.2登录操作,也就是连接xmpp服务器
- (void)connect {
if (self.xmppStream == nil) {
self.xmppStream = [[XMPPStream alloc] init];
[self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}
if (![self.xmppStream isConnected]) {
NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
XMPPJID *jid = [XMPPJID jidWithUser:username domain:@"lizhen" resource:@"Ework"];
[self.xmppStream setMyJID:jid];
[self.xmppStream setHostName:@"10.4.125.113"];
NSError *error = nil;
if (![self.xmppStream connect:&error]) {
NSLog(@"Connect Error: %@", [[error userInfo] description]);
}
}
}
connect成功之后会依次调用XMPPStreamDelegate的方法,首先调用
- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
然后
- (void)xmppStreamDidConnect:(XMPPStream *)sender
在该方法下面需要使用xmppStream 的authenticateWithPassword方法进行密码验证,成功的话会响应delegate的方法,就是下面这个
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
1.3上线
实现 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender 委托方法
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];
[self.xmppStream sendElement:presence];
}
1.4退出并断开连接
- (void)disconnect {
XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
[self.xmppStream sendElement:presence];
[self.xmppStream disconnect];
}
1.5好友状态
获取好友状态,通过实现
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
...
方法,当接收到 presence 标签的内容时,XMPPFramework 框架回调该方法










