【iOS XMPP】使用XMPPFramewok(二):用户登录
用户登录
准备工作
比较知名的开源XMPP服务器:一个是Openfire,一个是ejabberd
Openfire 使用 Java 语言编写,比较容易上手,地址:http://www.igniterealtime.org/projects/openfire/
ejabberd 使用 Erlang 语言编写,是一款非常知名的 Erlang 开源项目,地址:http://www.ejabberd.im/
安装 ejabberd,可以参考我的博客:【ejabberd】安装XMPP服务器ejabberd(Ubuntu 12.04)
搭建一个自己的 XMPP 服务器之后,就让我们开始吧!
连接服务器
1、新建一个 XMPPStream 对象,添加委托
添加委托方法 - (void)addDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue
参数 delegateQueue 为委托回调所使用的 GCD 队列,dispatch_get_main_queue() 获取主线程 GCD 队列
2、设置 JID 和 主机名
JID 一般由三部分构成:用户名,域名和资源名,例如 test@example.com/Anthony
如果没有设置主机名,则使用 JID 的域名作为主机名
端口号是可选的,默认是 5222
3、连接
- (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]); } } }
身份认证
实现 - (void)xmppStreamDidConnect:(XMPPStream *)sender 委托方法
连接服务器成功后,回调该方法
This method is called after the XML stream has been fully opened. More precisely, this method is called after an opening <xml/> and <stream:stream/> tag have been sent and received, and after the stream features have been received, and any required features have been fullfilled. At this point it's safe to begin communication with the server.
身份认证方法 - (BOOL)authenticateWithPassword:(NSString *)inPassword error:(NSError **)errPtr
- (void)xmppStreamDidConnect:(XMPPStream *)sender { NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"]; NSError *error = nil; if (![self.xmppStream authenticateWithPassword:password error:&error]) { NSLog(@"Authenticate Error: %@", [[error userInfo] description]); } }
上线
实现 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender 委托方法
身份认证成功后,回调该方法
This method is called after authentication has successfully finished.
If authentication fails for some reason, the xmppStream:didNotAuthenticate: method will be called instead.
新建一个 XMPPPresence 对象,类型为 available,发送!
- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender { XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"]; [self.xmppStream sendElement:presence]; }
退出并断开连接
新建一个 XMPPPresence 对象,类型为 unavailable,发送!
断开连接
- (void)disconnect { XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"]; [self.xmppStream sendElement:presence]; [self.xmppStream disconnect]; }
iOS 开发讨论群:82873648

本作品采用知识共享署名-非商业性使用 3.0 许可协议进行许可。
转载请署名李震(博客地址:http://www.cnblogs.com/dyingbleed/),且不得用于商业目的。
博客园博客已停止更新,博客地址:dyingbleed.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?