Introduction to the theory of XMPP in this blog is not to do, how to join the XMPP protocol before us to achieve communication? The basic knowledge of XMPP is described below, allowing us to achieve connected communication. The preparation is to have a server that supports the XMPP protocol, then register a test account with spark and finally communicate with our existing account and password via XMPP. As for how to enable the server to support the XMPP protocol, how to register an account with Spark is not the topic of this blog, the main article in this blog is how to use the XMPP protocol in our app.
Today's blog content is how to introduce xmppframework in the project, and in the app can connect and authenticate our account and password, OK, talk less, cut into today's topic.
I. Introduction of Xmppframework
1. Using XMPP of course, the framework of the import, or use Cocoapods to manage the third-party library, in the corresponding project with Cocoapods introduced Xmppframework, in the profile to add the corresponding version of the Xmppframework framework, and then pod Update the installation, the terminal is as follows:
2.update after the success, we can work with xmppframework.
Two. Use Xmppframework to connect to the server and authenticate the password
1. Declare and instantiate Xmppstream in Appdelegate, similar to Managedobjectcontext in CoreData when obtaining Xmppstream instances, the code below is given,
Initialize the Xmppsteam code as follows:
1 // XMPP Data Flow 2 @property (Strong, Nonatomic) Xmppstream * Xmppstream; 3 4 5 // Create Xmppstream 6 Self.xmppstream = [[Xmppstream alloc]init];
2. When using Xmppframework, because it uses a delegate callback, the Xmppstreamdelegate protocol is implemented in the corresponding controller, and then the corresponding method in the protocol is implemented.
(1). In the controller using Xmppstream, get the Xmppstream instance we created above through application delegate, the code is as follows:
1 // 2 uiapplication *application =< Span style= "color: #000000;" > [uiapplication Sharedapplication]; 3 Delegate = [application delegate 4 self.xmppstream = [ Xmppstream]; 5 //
Register callback 7 [Self.xmppstream Adddelegate:self Delegatequeue:dispatch_get_main_queue ()];
(2) After obtaining the XMPP stream, you can connect to the server, the connection server is divided into three parts, first splicing Xmppjid, and then add Jid to Xmppstream, the last connection. The code is as follows:
1 //connecting to a server2-(void) Xmppconnect3 {4 //1. Create Jid5Xmppjid *jid = [Xmppjid jidwithuser:@"Lizelusdut"Domain:my_domain resource:@"IPhone"];6 7 //2. Add the Jid to the Xmppsteam8 [Self.xmppstream Setmyjid:jid];9 Ten //connecting to a server OneNserror *error =Nil; A[Self.xmppstream connectwithtimeout:Tenerror:&ERROR]; - if(Error) { -NSLog (@"connection error:%@", [Error localizeddescription]); the } -}
(3) To implement a callback method after connecting the server (to authenticate the user password after connection), the code is as follows
1 //Post-Connection callbacks2-(void) Xmppstreamdidconnect: (Xmppstream *) Sender3 {4 //authenticated user name and password after successful connection5Nserror *error =Nil;6[Self.xmppstream Authenticatewithpassword:@"[email protected] #admin"error:&ERROR];7 if(Error) {8NSLog (@"Authentication error:%@", [Error localizeddescription]);9 }Ten}
(4) To implement the method of callback after successful authentication, the code is as follows:
// callback after successful authentication -(void) Xmppstreamdidauthenticate: (Xmppstream *) sender{ NSLog (@ " Landing Success " );}
(5) The method to be called after authentication failed, the code is as follows:
1 // callback after successful authentication 2 -(void) Xmppstream:sender didnotauthenticate: (ddxmlelement *) error
3 {
4 NSLog (@ " Login failed ");
5 }
Personal feeling blog lengthy is not conducive to others to consume their knowledge, so today's blog first so long, today is to connect the server and authentication user identity. will continue to update the content of the blog, until two apps can communicate between, I hope you continue to pay attention to.
iOS development using Xmppframework for instant communication (i)