Things to prepare before connecting to the server:
1. Build the XMPP server
2. Set the server address and port
[_xmppstream sethostname:@ "127.0.0.1"];
[_xmppstream sethostport:5222];
3. Call Connect
Key items:
jid-format must be "username" + "@" + "server Address" example: [email protected]
The connection server must be Jid set, if no account can set any value
The specific code is as follows:
-(BOOL) Connect: (nsstring*) User withpassword: (nsstring*) pwd{
if(User! =Nil) {User= [NSString stringWithFormat:@"%@@%@", User,_xmppstream.hostname]; } if(![_xmppstream isdisconnected]) { if(_islogined) {Nserror*error =Nil; Password=pwd; [Self xmppstream] Setmyjid:[xmppjid jidwithstring:user resource:@"iOS"]]; if(! [Self Xmppstream] Authenticatewithpassword:password error:&ERROR]) {NSLog (@"Error authenticating:%@", error); } } returnYES; } nsstring*myjid =user; NSString*mypassword =pwd; if(MyPassword! =Nil) {Password=mypassword; } if(Myjid! =Nil) {[_xmppstream Setmyjid:[xmppjid Jidwithstring:myjid resource:@"iOS"]]; }Else{[_xmppstream Setmyjid:[xmppjid jidwithstring:_xmppstream.hostname resource:@"iOS"]]; } nserror*error =Nil; [_xmppstream connectwithtimeout:Tenerror:&ERROR]; if(Error) {NSLog (@"Connection Failed%@", error); } returnYES;}
Callback results returned by the connection
///Connection Successful- (void) Xmppstreamdidconnect: (Xmppstream *) sender{NSLog (@"Connection Successful"); Isxmppconnected=YES; if(_islogined) {Nserror*error =Nil; if(! [Self Xmppstream] Authenticatewithpassword:password error:&ERROR]) {NSLog (@"Error authenticating:%@", error); } }}///Connection Failed- (void) Xmppstreamdiddisconnect: (Xmppstream *) sender Witherror: (Nserror *) error{NSLog (@"Connection Disconnect"); if(!isxmppconnected) { //Ddlogerror (@ "Unable to connect to server. Check xmppstream.hostname "); }}
Disconnect Connection
[_xmppstream disconnect];
iOS Development--xmppframewok framework based on XMPP Protocol (ii): Server connection