#import "AppDelegate.h"#import "XMPPFramework.h"/** Implement login in Appdelegate 1. Initialize Xmppstream 2. Connect to server [pass a Jid] 3. After the connection to the service is successful, the password authorization 4 is sent. After the authorization is successful, send the "online" message*/@interfaceAppdelegate () <XMPPStreamDelegate>{Xmppstream*_xmppstream;}//1. Initialize Xmppstream-(void) Setupxmppstream;//2. Connect to the server-(void) Connecttohost;//3. After the connection to the service is successful, then send the password authorization-(void) Sendpwdtohost;//4. After successful authorization, send "online" message-(void) Sendonlinetohost;@end@implementationappdelegate-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//when the program starts, it connects to the host.[self connecttohost]; returnYES;}#pragmaMark-Private method#pragmaMark Initializes the xmppstream-(void) setupxmppstream{_xmppstream=[[Xmppstream alloc] init]; //Set up proxy[_xmppstream adddelegate:self Delegatequeue:dispatch_get_global_queue (Dispatch_queue_priority_default,0)];}#pragmaMark connects to the server-(void) connecttohost{NSLog (@"start connecting to the server"); if(!_xmppstream) {[Self setupxmppstream]; } //Set the login user Jid//Resource Identity User Login client iphone androidXmppjid*myjid = [Xmppjid jidwithuser:@"Wangwu"Domain@"teacher.local"Resource@"iphone" ]; _xmppstream.myjid=Myjid; //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; //ConnectionNserror *err =Nil; if(! [_xmppstream Connectwithtimeout:xmppstreamtimeoutnone error:&Err]) {NSLog (@"%@", err); } }#pragmaMark connects to the service successfully, then sends the password authorization-(void) sendpwdtohost{NSLog (@"re-send password authorization"); Nserror*err =Nil; [_xmppstream Authenticatewithpassword:@"123456"error:&err]; if(Err) {NSLog (@"%@", err); }}#pragmaAfter Mark's authorization is successful, send an "online" message-(void) sendonlinetohost{NSLog (@"Send online message"); Xmpppresence*presence =[xmpppresence presence]; NSLog (@"%@", presence); [_xmppstream sendelement:presence]; }#pragmaMark-xmppstream's agent#pragmaMark successfully connected to the host-(void) Xmppstreamdidconnect: (Xmppstream *) sender{NSLog (@"successful connection to host"); //after the host connection is successful, send a password to authorize[self sendpwdtohost];}#pragmaMark disconnects from the host-(void) Xmppstreamdiddisconnect: (Xmppstream *) sender Witherror: (Nserror *) error{//If there is an error, the connection failsNSLog (@"disconnecting from the host%@", error); }#pragmaMark Authorized Success-(void) Xmppstreamdidauthenticate: (Xmppstream *) sender{NSLog (@"Authorized Success"); [Self sendonlinetohost];}#pragmaMark Authorization Failed-(void) Xmppstream: (Xmppstream *) sender Didnotauthenticate: (Ddxmlelement *) error{NSLog (@"Authorization Failure%@", error);}#pragmaMark-Public Method-(void) logout{//1. "Send" Offline "message"Xmpppresence *offline = [xmpppresence presencewithtype:@"unavailable"]; [_xmppstream Sendelement:offline]; //2. Disconnecting from the server[_xmppstream disconnect];}@end
IOS XMPP (Instant Messaging framework)