Transferred from: http://www.cnblogs.com/dyingbleed/archive/2013/05/10/3069397.html
User Login
Preparatory work
More well-known open source XMPP servers: One is OpenFire, one is Ejabberd
OpenFire in the Java language, relatively easy to use, address: http://www.igniterealtime.org/projects/openfire/
Ejabberd is a well-known Erlang open Source project, written in Erlang, address: http://www.ejabberd.im/
To install Ejabberd, you can refer to my blog: "ejabberd" Install XMPP server Ejabberd (Ubuntu 12.04)
Once you've built your own XMPP server, let's get started!
Connecting to a server
1. Create a new Xmppstream object and add a delegate
Add Delegate Method-(void) AddDelegate: (ID) Delegate Delegatequeue: (dispatch_queue_t) delegatequeue
Parameter delegatequeue is the GCD queue used by the delegate callback, Dispatch_get_main_queue() gets the main thread GCD queue
2. Set the JID and host name
JID generally consists of three parts: User name, domain name, and resource name, for example [email protected]/anthony
If the hostname is not set, the JID domain name is used as the host name
Port number is optional, default is 5222
3. Connection
-(void) Connect { if (Self.xmppstream = = nil) { self.xmppstream = [[Xmppstream alloc] init]; [Self.xmppstream adddelegate:self Delegatequeue:dispatch_get_main_queue ()]; } if (![ Self.xmppstream isconnected]) { NSString *username = [[Nsuserdefaults standarduserdefaults] objectForKey:@ " Username "]; Xmppjid *jid = [Xmppjid jidwithuser:username domain:@ "Lizhen" resource:@ "Ework"]; [Self.xmppstream Setmyjid:jid]; [Self.xmppstream sethostname:@ "10.4.125.113"]; Nserror *error = nil; if (![ Self.xmppstream Connect:&error]) { NSLog (@ "Connect error:%@", [[Error UserInfo] description]);}}}
Identity verification
Implementation -(void) Xmppstreamdidconnect: (xmppstream *) sender Delegate method
After the connection server succeeds, callback the method
This method was called after the XML stream had been fully opened. More precisely, this method was called after an opening <xml/> and <stream:stream/> tag has been sent and rece Ived, and after the stream features has been received, and any required features has been fullfilled. At this point it's safe to begin communication with the server.
Identity authentication Method -(BOOL) Authenticatewithpassword: (nsstring *) Inpassword error: (nserror *) errptr
-(void) Xmppstreamdidconnect: (Xmppstream *) Sender { NSString *password = [[Nsuserdefaults standarduserdefaults] objectforkey:@ "Password"]; Nserror *error = nil; if (![ Self.xmppstream Authenticatewithpassword:password Error:&error]) { NSLog (@ "Authenticate error:%@", [[Error UserInfo] description]);} }
Online
Implementation -(void) Xmppstreamdidauthenticate: (xmppstream *) sender Delegate method
After successful authentication, callback the method
This method was called after authentication have successfully finished.
If authentication fails for some reason, the xmppStream:didNotAuthenticate:method would be called instead.
Create a new Xmpppresence object, type available, send!
-(void) Xmppstreamdidauthenticate: (Xmppstream *) Sender { xmpppresence *presence = [xmpppresence presencewithtype: @ "available"]; [Self.xmppstream sendelement:presence];}
Exit and Disconnect
Create a new Xmpppresence object, type unavailable, send!
Disconnect Connection
-(void) Disconnect { xmpppresence *presence = [xmpppresence presencewithtype:@ "unavailable"]; [Self.xmppstream sendelement:presence]; [Self.xmppstream disconnect];}
"IOS XMPP" using Xmppframewok (ii): User Login