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 Xmppstream 2-(void) Setupxmppstream; 3 4 5//2. Connect to server 6-(void) connecttohost; 7 8//3. After connecting to the service successfully, send password authorization 9-(void) SENDPWDTOHOST;10 11 12//4. After the authorization is successful , 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 //program one starts to 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 #pragma mark connects to server 2-(void) connecttohost{3 NSLog (@ "Start connecting to Server"), 4 if (!_xmppstream) {5 [self setupxmppst Ream]; 6 } 7 8 9 //Set login user JID10 //resource identity User Login client iphone android11 Xmppjid *myjid = [ Xmppjid jidwithuser:@ "Wangwu" domain:@ "teacher.local" resource:@ "iphone"];13 _xmppstream.myjid = myJID;14 15 //Set the server domain name to _xmppstream.hostname = @ "teacher.local";//not only the domain name, but also the IP address. //Set port If the server port is 5222, you can omit _xmppstream.hostport = 5222;20 //Connect to nserror *err = nil;23 if (![ _xmppstream Connectwithtimeout:xmppstreamtimeoutnone Error:&err]) { NSLog (@ "%@", err); }26 27}
Agent method for connecting to host
1 #pragma mark-xmppstream Agent 2 #pragma mark successfully connected to the host 3-(void) Xmppstreamdidconnect: (Xmppstream *) sender{4 NSLog (@ "with the main Machine connected successfully "); 5 6 //After the host connection is successful, send password to authorize 7 [self sendpwdtohost]; 8} 9 #pragma mark disconnected from host-(void) Xmppstreamdiddisconnect: (Xmppstream *) sender Witherror: (Nserror *) error{11 //If there is an error, the connection fails on behalf of the NSLog (@ " Disconnect from host%@ ", error); 14}
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 Agent 8 [_xmppstream adddelegate:self Delegatequeue:dispatch_get_global_queue ( Dispatch_queue_priority_default, 0)];9}
Five: Send password authorization
1 #pragma mark connects to the service successfully, then sends the password Authorization 2-(void) sendpwdtohost{3 NSLog (@ "Resend Password Authorization"), 4 nserror *err = Nil;5 [_ Xmppstream authenticatewithpassword:@ "123456" error:&err];6 if (err) {7 NSLog (@ "%@", err); 8 }9}
Six: After authorization succeeds, send status message
1 #pragma mark authorized success, send "online" Message 2-(void) sendonlinetohost{3 4 NSLog (@ "send online message"); 5 Xmpppresence * presence = [Xmpppresence presence]; 6 NSLog (@ "%@", Presence), 7 8 [_xmppstream sendelement:presence]; 9 11}
Seven: Proxy method for authorization success and failure
1 #pragma mark Authorized success 2-(void) Xmppstreamdidauthenticate: (Xmppstream *) sender{3 NSLog (@ "authorized success"); 4 5 [self Sendonlinetohost]; 6} 7 8 9 #pragma mark authorization failure (void) Xmppstream: (Xmppstream *) sender Didnotauthenticate: (ddxmlelement *) error{ NSLog (@ "Authorization failed%@", error); 12}
Cancellation
One: A method of defining and implementing a logoff in an agent
1//Logoff 2-(void) logout;
1 #pragma mark-Public Method 2-(void) logout{3 //1. "Send" Offline "message" 4 xmpppresence *offline = [xmpppresence presencewithtype: @ "Unavailable"];5 [_xmppstream sendelement:offline];6 7 //2. Disconnecting from server 8 [_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 logoff 3 appdelegate *app = [uiapplication Sharedapplication].delegate;4 [App logout];5 6}
iOS Development--Network programming OC & (ii) XMPP implementation user Login and logoff