XMPP 編程 iOS平台(一) 配置 與 登陸

來源:互聯網
上載者:User

標籤:ios   xmpp   登陸   退出   配置   

這裡記錄下我配置XMPP的過程

首先下載XMPP:https://github.com/robbiehanson/XMPPFramework/

將如下檔案考入過程中:


添加依賴架構


添加XML架構的依賴路徑:


這樣就配置完了。下面說下如何進行登陸。當然伺服器要自己先搭好,如果沒有搭好可以看前一篇。

登陸代碼:

@interface XMPPModel()<XMPPStreamDelegate>@property (nonatomic , strong) XMPPStream *xmppStream;@end

- (BOOL) connect{    [self setupStream];    //這裡為了減少代碼數量所以直接在這裡寫死了。大家測試時直接修改就好。    // 從本地取得使用者名稱和密碼和伺服器位址        NSString *userId = @"dujia"; //使用者名稱    NSString *password = @"zxcvbnm"; //密碼    NSString *server = @"10.86.102.70"; //伺服器的IP                    if (![_xmppStream isDisconnected]) {        return  YES;    }        //  下面3個參數分別是  使用者名稱  domain 網域名稱   resource 這個ming    XMPPJID *jid = [XMPPJID jidWithUser:userId domain:@"shenqi" resource:@"Ework"];        [_xmppStream setMyJID:jid];    [_xmppStream setHostName:server];    [_xmppStream setHostPort:5222];            NSError *error = nil;    if (![_xmppStream connectWithTimeout:3.0 error:&error]) {        NSLog(@"%@",error.userInfo);    }    return YES;}- (void)setupStream{    _xmppStream = [[XMPPStream alloc] init];    [_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];    }

下面是串連成功的回調。在下面這步驟才驗證密碼:

- (void)xmppStreamDidConnect:(XMPPStream *)sender{    NSError *error = nil;    if (![sender authenticateWithPassword:@"zxcvbnm" error:&error]) {        NSLog(@"%@",error.userInfo);    }    }

這樣伺服器頁面會有如下顯示:


這樣就登陸成功了。下面再說下退出。

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

代碼下載:http://pan.baidu.com/s/1hqiUfmG


XMPP 編程 iOS平台(一) 配置 與 登陸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.