iOS開發——網路編程OC篇&(二)XMPP實現使用者登入與登出

來源:互聯網
上載者:User

標籤:

 XMPP實現使用者登入與登出

 

登入:

 

步驟:
 * 在AppDelegate實現登入

  •  1. 初始化XMPPStream
  •  2. 串連到伺服器[傳一個JID]
  •  3. 串連到服務成功後,再發送密碼授權
  •  4. 授權成功後,發送"線上" 訊息

 

一:匯入架構,根據上一篇文章的說明去匯入相應的庫與檔案

二:定義一個XMPP的成員變數

 1 @interface AppDelegate ()<XMPPStreamDelegate>{ 2 XMPPStream *_xmppStream; 3 }

三:按步驟在代理方法中聲明四個需要實現的方法

 1 // 1. 初始化XMPPStream 2 -(void)setupXMPPStream; 3  4  5 // 2.串連到伺服器 6 -(void)connectToHost; 7  8 // 3.串連到服務成功後,再發送密碼授權 9 -(void)sendPwdToHost;10 11 12 // 4.授權成功後,發送"線上" 訊息13 -(void)sendOnlineToHost;
 四:在程式啟動的時候調用連結的伺服器主機的方法
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {2     3     // 程式一啟動就串連到主機4     [self connectToHost];5     return YES;6 }

連結的伺服器主機方法的實現,在連結的伺服器主機方法中,如果連結成功則初始化XMPPStream

 1 #pragma mark 串連到伺服器 2 -(void)connectToHost{ 3     NSLog(@"開始串連到伺服器"); 4     if (!_xmppStream) { 5         [self setupXMPPStream]; 6     } 7      8      9     // 設定登入使用者JID10     //resource 標識使用者登入的用戶端 iphone android11     12     XMPPJID *myJID = [XMPPJID jidWithUser:@"wangwu" domain:@"teacher.local" resource:@"iphone" ];13     _xmppStream.myJID = myJID;14 15     // 設定伺服器網域名稱16     _xmppStream.hostName = @"teacher.local";//不僅可以是網域名稱,還可是IP地址17     18     // 設定連接埠 如果伺服器連接埠是5222,可以省略19     _xmppStream.hostPort = 5222;20     21     // 串連22     NSError *err = nil;23     if(![_xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&err]){24         NSLog(@"%@",err);25     }26     27 }

與主機串連的代理方法

 1 #pragma mark -XMPPStream的代理 2 #pragma mark 與主機串連成功 3 -(void)xmppStreamDidConnect:(XMPPStream *)sender{ 4     NSLog(@"與主機串連成功"); 5      6     // 主機串連成功後,發送密碼進行授權 7     [self sendPwdToHost]; 8 } 9 #pragma mark  與主機中斷連線10 -(void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error{11     // 如果有錯誤,代表串連失敗12     NSLog(@"與主機中斷連線 %@",error);13         14 }

 

初始化XMPPStream方法的實現

1 #pragma mark  -私人方法2 #pragma mark 初始化XMPPStream3 -(void)setupXMPPStream{4     5     _xmppStream = [[XMPPStream alloc] init];6     7     // 設定代理8     [_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];9 }

五:發送密碼授權

1 #pragma mark 串連到服務成功後,再發送密碼授權2 -(void)sendPwdToHost{3     NSLog(@"再發送密碼授權");4     NSError *err = nil;5     [_xmppStream authenticateWithPassword:@"123456" error:&err];6     if (err) {7         NSLog(@"%@",err);8     }9 }

六:授權成功後,發送狀態訊息

 1 #pragma mark  授權成功後,發送"線上" 訊息 2 -(void)sendOnlineToHost{ 3      4     NSLog(@"發送 線上 訊息"); 5     XMPPPresence *presence = [XMPPPresence presence]; 6     NSLog(@"%@",presence); 7      8     [_xmppStream sendElement:presence]; 9     10     11 }

七:授權成功與失敗的代理方法

 1 #pragma mark 授權成功 2 -(void)xmppStreamDidAuthenticate:(XMPPStream *)sender{ 3     NSLog(@"授權成功"); 4      5     [self sendOnlineToHost]; 6 } 7  8  9 #pragma mark 授權失敗10 -(void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error{11      NSLog(@"授權失敗 %@",error);12 }

登出

 

一:在代理中定義並且實現一個登出的方法

 1 // 登出 2 -(void)logout; 

1 #pragma mark -公用方法2 -(void)logout{3     // 1." 發送 "離線" 訊息"4     XMPPPresence *offline = [XMPPPresence presenceWithType:@"unavailable"];5     [_xmppStream sendElement:offline];6     7     // 2. 與伺服器中斷連線8     [_xmppStream disconnect];9 }

二:實現登出--這裡我們只實現點擊螢幕就登出並且推出

1 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{2     // 做登出3     AppDelegate *app = [UIApplication sharedApplication].delegate;4     [app logout];5     6 }

 

 

 

 

iOS開發——網路編程OC篇&(二)XMPP實現使用者登入與登出

聯繫我們

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