iOS Development--Network programming OC & (ii) XMPP implementation user Login and logoff

Source: Internet
Author: User

XMPP implements user logon and logoff

Login:

Steps:
* Login is implemented in Appdelegate

    • 1. Initialize Xmppstream
    • 2. Connect to the server [pass a Jid]
    • 3. After the connection to the service is successful, then send the password authorization
    • 4. After successful authorization, send "online" message

One: Import the framework, according to the previous article to import the corresponding library and file

Two: Define a member variable for XMPP

1 @interface appdelegate () <XMPPStreamDelegate>{ 2 Xmppstream *_ Xmppstream; 3 }

Three: Follow the steps in the proxy method to declare four methods that need to be implemented

1 //1. Initialize Xmppstream2-(void) Setupxmppstream;3 4 5 //2. Connect to the server6-(void) Connecttohost;7 8 //3. After the connection to the service is successful, then send the password authorization9-(void) Sendpwdtohost;Ten  One  A //4. After successful authorization, send "online" message --(void) Sendonlinetohost;
Four: The method of invoking the linked server host when the program starts
1 -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {2     3     //  When the program starts, connect to host 4    [self Connecttohost]; 5     return YES; 6 }

The implementation of the linked server host method, in the linked server host method, initializes the Xmppstream if the link succeeds

1 #pragmaMark connects to the server2-(void) connecttohost{3NSLog (@"start connecting to the server");4     if(!_xmppstream) {5 [self setupxmppstream];6     }7     8     9     //Set the login user JidTen     //Resource Identity User Login client iphone android One      AXmppjid *myjid = [Xmppjid jidwithuser:@"Wangwu"Domain@"teacher.local"Resource@"iphone" ]; -_xmppstream.myjid =Myjid; -  the     //set the server domain name -_xmppstream.hostname =@"teacher.local";//not only can it be a domain name, but also an IP address -      -     //set the port if the server port is 5222, you can omit the +_xmppstream.hostport =5222; -      +     //Connection ANserror *err =Nil; at     if(! [_xmppstream Connectwithtimeout:xmppstreamtimeoutnone error:&Err]) { -NSLog (@"%@", err); -     } -      -}

Agent method for connecting to host

1 #pragmaMark-xmppstream's agent2 #pragmaMark successfully connected to host3-(void) Xmppstreamdidconnect: (Xmppstream *) sender{4NSLog (@"successful connection to host");5     6     //after the host connection is successful, send a password to authorize7 [self sendpwdtohost];8 }9 #pragmaMark is disconnected from the hostTen-(void) Xmppstreamdiddisconnect: (Xmppstream *) sender Witherror: (Nserror *) error{ One     //If there is an error, the connection fails ANSLog (@"disconnecting from the host%@", error); -          -}

Implementation of initialization Xmppstream method

1 #pragma Mark  -Private Method 2#pragma Mark initialize Xmppstream3 -(void) setupxmppstream{4     5     _xmppstream = [[Xmppstream alloc] init]; 6     7     // Set up proxy 8     0 )]; 9 }

Five: Send password authorization

1 #pragmaMark connects to a successful service and then sends a password authorization2-(void) sendpwdtohost{3NSLog (@"re-send password authorization");4Nserror *err =Nil;5[_xmppstream Authenticatewithpassword:@"123456"error:&err];6     if(err) {7NSLog (@"%@", err);8     }9}

Six: After authorization succeeds, send status message

1 #pragmaAfter Mark's authorization is successful, send an "online" message2-(void) sendonlinetohost{3     4NSLog (@"Send online message");5Xmpppresence *presence =[xmpppresence presence];6NSLog (@"%@", presence);7     8 [_xmppstream sendelement:presence];9     Ten      One}

Seven: Proxy method for authorization success and failure

1 #pragmaMark Authorized Success2-(void) Xmppstreamdidauthenticate: (Xmppstream *) sender{3NSLog (@"Authorized Success");4     5 [self sendonlinetohost];6 }7 8 9 #pragmaMark's authorization failed.Ten-(void) Xmppstream: (Xmppstream *) sender Didnotauthenticate: (Ddxmlelement *) error{ OneNSLog (@"Authorization Failure%@", error); A}

Cancellation

One: A method of defining and implementing a logoff in an agent

1 // logoff 2 -(void) logout;

1 #pragmaMark-Public method2-(void) logout{3     //1. "Send" Offline "message"4Xmpppresence *offline = [xmpppresence presencewithtype:@"unavailable"];5 [_xmppstream sendelement:offline];6     7     //2. Disconnecting from the server8 [_xmppstream disconnect];9}

Two: Realize logout--here we only realize the click on the screen to logout and launch

1 -(void) Touchesbegan: (Nsset *) touches withevent: (uievent *)event{2     //  do logout 3     Appdelegate *app = [UIApplication sharedapplication]. Delegate ; 4     [app logout]; 5     6 }

iOS Development--Network programming OC & (ii) XMPP implementation user Login and logoff

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.