1. Create a singleton mode to manage the connection and operation of XMPP
1+ (Xmppmanager *) Share2 {3 StaticXmppmanager *_share=Nil;4 Staticdispatch_once_t Oncetoken;5Dispatch_once (&oncetoken, ^{6_share=[[Xmppmanager alloc] init];7 });8 return_share;9 }Ten-(ID) Init One { A if(self=[Super Init]) { - [self setupstream]; - } the returnSelf ; - } -- (void) Setupstream - { +Xmppstream =[[Xmppstream alloc] init]; - [Xmppstream adddelegate:self delegatequeue:dispatch_get_main_queue ()]; +[Xmppstream SetHostName:@"127.0.0.1"]; A[Xmppstream Sethostport:5222]; at}
2. Login and registration are required to connect to the server using the user name.
-(void) login{[self connect];}-(void) reg{Isreg=YES; [Self connect];}-(BOOL) connect{if(xmppstream.isconnected) {[Self disconnect]; } if(![Xmppstream isdisconnected]) { returnYES; } if(Self.username = =Nil) { returnNO; } [Xmppstream Setmyjid:[xmppjid jidWithString:self.userName]; Nserror*error =Nil; if(! [Xmppstream Connectwithtimeout:xmppstreamtimeoutnone error:&ERROR]) {Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Error connecting"message:@"See Console for error details." Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil]; [Alertview show]; returnNO; } returnYES;}
3. In the connection agent to determine whether to log in or register, send different commands
//successfully connected server- (void) Xmppstream: (Xmppstream *) sender Socketdidconnect: (Gcdasyncsocket *) socket{Ddlogverbose (@"%@: %@", This_file, This_method);}//failed to connect to server- (void) Xmppstreamdiddisconnect: (Xmppstream *) sender Witherror: (Nserror *) error{Ddlogverbose (@"%@: %@", This_file, This_method);}- (void) Xmppstreamdidconnect: (Xmppstream *) sender{Ddlogverbose (@"%@: %@", This_file, This_method); Nserror*error =Nil; if(isreg) {if(! [Xmppstream registerWithPassword:self.pwd error:&ERROR]) { } } Else { if(! [Xmppstream authenticateWithPassword:self.pwd error:&ERROR]) { } } }
4. Handling Login or registration agent functions
//Login Successful- (void) Xmppstreamdidauthenticate: (Xmppstream *) sender{Ddlogverbose (@"%@: %@", This_file, This_method); [Self goonline]; Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Login Successful"message:@"Login Successful" Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil]; [Alertview show];}//Logon Failure- (void) Xmppstream: (Xmppstream *) sender Didnotauthenticate: (Nsxmlelement *) error{Ddlogverbose (@"%@: %@", This_file, This_method); Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Password Error"message:@"Password Error" Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil]; [Alertview show]; }//Registration Successful- (void) Xmppstreamdidregister: (Xmppstream *) sender{Ddlogverbose (@"%@: %@", This_file, This_method); Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Registration Successful"message:@"Registration Successful" Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil]; [Alertview show]; Isreg=NO;}//Registration Failed- (void) Xmppstream: (Xmppstream *) sender Didnotregister: (Nsxmlelement *) error{Ddlogverbose (@"%@: %@", This_file, This_method); NSLog (@"%@", [[Error Elementforname:@"Error"] stringvalue]); Uialertview*alertview = [[Uialertview alloc] Initwithtitle:@"Registration Failed"message:@"Registration Failed" Delegate: nil Cancelbuttontitle:@"Ok"Otherbuttontitles:nil]; [Alertview show]; Isreg=NO;}
5. Login or register the username must be "[email protected] hostname" This format
Reprint Please specify: http://www.cnblogs.com/bandy/p/3756492.html