日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【iOS XMPP】使用XMPPFramewok(二):用户登录

發(fā)布時(shí)間:2024/9/30 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【iOS XMPP】使用XMPPFramewok(二):用户登录 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

用戶登錄

?

準(zhǔn)備工作

比較知名的開源XMPP服務(wù)器:一個(gè)是Openfire,一個(gè)是ejabberd

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

ejabberd 使用 Erlang 語言編寫,是一款非常知名的 Erlang 開源項(xiàng)目,地址:http://www.ejabberd.im/

安裝 ejabberd,可以參考我的博客:【ejabberd】安裝XMPP服務(wù)器ejabberd(Ubuntu 12.04)

搭建一個(gè)自己的 XMPP 服務(wù)器之后,就讓我們開始吧!

?

連接服務(wù)器

1、新建一個(gè) XMPPStream 對(duì)象,添加委托

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

參數(shù) delegateQueue 為委托回調(diào)所使用的 GCD 隊(duì)列,dispatch_get_main_queue() 獲取主線程 GCD 隊(duì)列

2、設(shè)置 JID 和 主機(jī)名

JID 一般由三部分構(gòu)成:用戶名,域名和資源名,例如 test@example.com/Anthony

如果沒有設(shè)置主機(jī)名,則使用 JID 的域名作為主機(jī)名

端口號(hào)是可選的,默認(rèn)是 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]);}} }

?

身份認(rèn)證

實(shí)現(xiàn)?- (void)xmppStreamDidConnect:(XMPPStream?*)sender?委托方法

連接服務(wù)器成功后,回調(diào)該方法

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.

身份認(rèn)證方法?- (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]);} }

?

上線

實(shí)現(xiàn)?- (void)xmppStreamDidAuthenticate:(XMPPStream?*)sender?委托方法

身份認(rèn)證成功后,回調(diào)該方法

This method is called after authentication has successfully finished.?

If authentication fails for some reason, the xmppStream:didNotAuthenticate: method will be called instead.

新建一個(gè) XMPPPresence 對(duì)象,類型為 available,發(fā)送!

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {XMPPPresence *presence = [XMPPPresence presenceWithType:@"available"];[self.xmppStream sendElement:presence]; }

?

退出并斷開連接

新建一個(gè)?XMPPPresence 對(duì)象,類型為 unavailable,發(fā)送!

斷開連接

- (void)disconnect {XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];[self.xmppStream sendElement:presence];[self.xmppStream disconnect]; }

?

總結(jié)

以上是生活随笔為你收集整理的【iOS XMPP】使用XMPPFramewok(二):用户登录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。