码迷,mamicode.com
首页 > 移动开发 > 详细

[iOS XMPP] iOS XMPP 登录

时间:2015-04-02 11:44:50      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:xmpp 用户登录   ios   

一:搭建一个即时聊天服务器推荐一下两种,搭建方法大家自行百度一下,有很多详细的教程

Openfire 使用 Java 语言编写,比较容易上手,地址:http://www.igniterealtime.org/projects/openfire/

ejabberd 使用 Erlang 语言编写,是一款非常知名的 Erlang 开源项目,地址:http://www.ejabberd.im/


二:开始进行登录操作

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 XMPP] iOS XMPP 登录

标签:xmpp 用户登录   ios   

原文地址:http://blog.csdn.net/wxs0124/article/details/44829389

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!