【iOS XMPP】使用XMPPFramewok(二):用戶登陸

轉自:http://www.cnblogs.com/dyingbleed/archive/2013/05/10/3069397.htmlhtml

用戶登陸服務器

 

準備工做dom

比較知名的開源XMPP服務器:一個是Openfire,一個是ejabberdpost

Openfire 使用 Java 語言編寫,比較容易上手,地址:http://www.igniterealtime.org/projects/openfire/ui

ejabberd 使用 Erlang 語言編寫,是一款很是知名的 Erlang 開源項目,地址:http://www.ejabberd.im/this

安裝 ejabberd,能夠參考個人博客:【ejabberd】安裝XMPP服務器ejabberd(Ubuntu 12.04)spa

搭建一個本身的 XMPP 服務器以後,就讓咱們開始吧!線程

 

鏈接服務器code

一、新建一個 XMPPStream 對象,添加委託server

添加委託方法 - (void)addDelegate:(id)delegate delegateQueue:(dispatch_queue_t)delegateQueue

參數 delegateQueue 爲委託回調所使用的 GCD 隊列,dispatch_get_main_queue() 獲取主線程 GCD 隊列

二、設置 JID 和 主機名

JID 通常由三部分構成:用戶名,域名和資源名,例如 test@example.com/Anthony

若是沒有設置主機名,則使用 JID 的域名做爲主機名

端口號是可選的,默認是 5222

三、鏈接

複製代碼
- (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];
}

 

 

Android 開發討論羣:84778336 
iOS 開發討論羣:82873648 

知識共享許可協議
本做品採用知識共享署名-非商業性使用 3.0 許可協議進行許可。
轉載請署名李震(博客地址:http://www.cnblogs.com/dyingbleed/),且不得用於商業目的。
相關文章
相關標籤/搜索