iOS Development uses xmppframework for instant communication
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 stream 2 @property (strong, Nonatomic) Xmppstream * xmppstream;3 4 5 //create XMPPSTREAM6 self.xmppstream = [[XMPP Stream 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 //Get the app's Xmppsteam (via application in a single sample) 2 uiapplication *application = [UIApplication sharedapplication];3 ID delegate = [application delegate];4 self.xmppstream = [Delegate xmppstream];5 6 //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//Connection server 2-(void) Xmppconnect 3 {4 //1. Create Jid 5 xmppjid *jid = [Xmppjid jidwithuser:@ "Lizelusdut" Domain:my_doma In resource:@ "IPhone"]; 6 7 //2. Add Jid to Xmppsteam 8 [Self.xmppstream Setmyjid:jid]; 9 //connection server nserror *error = Nil;12 [Self.xmppstream connectwithtimeout:10 error:&error];13 if (error) { NSLog (@ "Connection error:%@", [ Error localizeddescription]); }16}
(3) To implement a callback method after connecting the server (to authenticate the user password after connection), the code is as follows
1//After connection callback 2-(void) Xmppstreamdidconnect: (Xmppstream *) Sender 3 {4 //Successful after connection authentication username and password 5 nserror *error = nil; 6
[self.xmppstream authenticatewithpassword:@ "[email protected] #admin" error:&error]; 7 if (error) {8 NSLog (@ "Authentication error:%@", [Error localizeddescription]); 9 }10}
(4) To implement the method of callback after successful authentication, the code is as follows:
Callback after successful authentication-(void) Xmppstreamdidauthenticate: (Xmppstream *) sender{ NSLog (@ "Login successful");}
(5) The method to be called after authentication failed, the code is as follows:
1//The callback after successful authentication 2-(void) Xmppstream:sender didnotauthenticate: (ddxmlelement *) error
3 {
4 NSLog (@ "Login failed");
5}
iOS development uses xmppframework for instant communication