[iOS XMPP] iOS XMPP 登入

來源:互聯網
上載者:User

[iOS XMPP] iOS XMPP 登入

一:搭建一個即時聊天伺服器推薦一下兩種,搭建方法大家自行百度一下,有很多詳細的教程

Openfire 使用 Java 語言編寫,比較容易上手,

ejabberd 使用 Erlang 語言編寫,是一款非常知名的 Erlang 開源項目,

 

二:開始進行登入操作

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 and 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];}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.