XMPPFramework development (two): project configuration and login registration

XMPPFramework development (two): project configuration and login registration

Make things Preface

In a previous post when it comes to build about Openfire server, this blog from the login registration started, in fact, found that many online XMPPFramework using the blog is simple to say all of them are proxy for what, a variety of interface between the logic of no more detailed explanation, use the Sao from the XMPPFramework building to prepare various proxy method and a simple chat App logic implementation, detail explanation. Well, for example to write their own SDChat also wrote for several days, there are still many Bug. I’m going to update, while Bug at the same time, while updating blog. I hope you understand.

XMPPFramework development (two): project configuration and login registration

XMPPFramework engineering configuration

  • XMPPFramework this I did not use cocospod, but manually manually import the folder. We can directly from the SDChat XMPPFramework dragged to their project, you can also download from Github source code in place
XMPPFramework development (two): project configuration and login registration
  • After we import the XMPPFramework into the project, we need to manually import the following two libraries. Libxml2.tbd, libresolv.tbd, XMPPFramework development (two): project configuration and login registration,
  • The basic configuration is complete, but we need to complete the XMPPConfig.h configuration file server. We need to project in the configuration of the project there are so few, are port IP address, server server (the default is 5222), the openfire server name, resource (resource, this can be defined).
    XMPPFramework development (two): project configuration and login registration

    server name we can login openfire management page, view and edit.
    XMPPFramework development (two): project configuration and login registration

    if the openfire server is built in the Mac, the IP address of the server can be viewed in the network preferences.
    XMPPFramework development (two): project configuration and login registration
    configuration there is a need to pay attention to, and if the openfire server is built in the m AC, server and mobile terminal must be in the same LAN. Otherwise, the application is not connected to the server. The two wireless network, every time MAC changes, IP is likely to change, if the latter does not connect, please check whether the change in openfire server IP.

The configuration completion example is as follows

#ifndef XMPPSample_XMPPConfig_h #define XMPPSample_XMPPConfig_h / / openfire server IP address kHostName @ #define "" / / openfire server port 5222 #define kHostPort / / openfire 5222 default server name #define @ kDomin "" / / resource (resource) #define @ kResource "IOS" #endif
  • The info.plist file also needs some modifications, but basically it doesn’t have much to do with XMPPFramework, and we’ll explain it in the specific module

Login function implementation of XMPPFramework

Since XMPPFramework has more functions, we need to add a single instance of managing SDXmppManager to manage all the functions of XMPPFramework. We import the #import XMPPFramework.h in the SDXmppManager.h file

We first take a look at the login interface in SDchat.~~~ fevered style

XMPPFramework development (two): project configuration and login registration

In XMPPFramework, the login registration function is mostly done by the XMPPStream class. We create a XMPPStream object property in the SDXmppManager singleton to manage login registration for the entire application

@property (nonatomic, strong) XMPPStream *stream;

Then we initialize the XMPPStream object property during the single instance initialization, set the IP address as well as the port number, and specify the proxy object

Self.stream [[XMPPStream = alloc]init]; self.stream.hostName = kHostName; self.stream.hostPort = kHostPort; / / set the stream proxy [self.stream () addDelegate:self delegateQueue:dispatch_get_main_queue];

Well, the basic preparation work is done, then. We first look at the specific login flow chart, and then in the SDChat than to login code in detail. (the size of the flow chart is a little big, if not clear, please download to a local view.)

XMPPFramework development (two): project configuration and login registration

The flow chart in almost all of the code in the SDXmppManager operation is completed in the single case. The first login is mainly divided into two steps, the first step is to connect to the server, the second step authentication password. The flow chart of two most split into many small steps.

We look at how to connect to the server. As shown in the following code, we need to decide whether you have a connection to the server. If the connection is already need to disconnect, and then use the server name, login name, resource name (omitted) configured only by user generated ID based on jabber protocol, and the configuration to stream. Then call – (BOOL) connectWithTimeout: (NSTimeInterval) timeout error: (NSError) errPtr method to connect to the server.

With the establishment of the linked server / / - (void) connectToServerWintUser: (NSString *) name{if ([self.stream isConnected]) {[self.stream disconnect]}; //jid jabberID, jabber protocol is generated only by username = [XMPPJID jidWithUser:name ID self.stream.myJID domain:kDomin resource:kResource] based on NSLog; (@ "% @", self.stream.myJID); NSError *error = nil; / / set the link with the server. [self.stream connectWithTimeout:30.0f error:& error]; if (error! = Nil) {NSLog (@ "connection failed!");}}

If the above verification succeeds, it will call the XMPPStream – (void) xmppStreamDidConnect: (XMPPStream * sender) the agent method, we need to verify the password on the inside. The password is simple only need to call the [self.stream authenticateWithPassword: self.password error:& error]; this line of code. The details are as follows. Because the registration and login so similar. They need to put together.

/ / establish a connection with the server - (void) xmppStreamDidConnect: (XMPPStream *) sender{NSError *error = nil; switch (self.type) case DoLgin: [self.stream authenticateWithPassword:self.password {error:& error]; if (error! = Nil) {NSLog (@ "authentication error!");} break case DoRegiser: [self.stream registerWithPassword:self.regiserPassword; error:& error]; if (error! = Nil) {NSLog (@ "registration error!");} break;}}

Verify completed, we need some interface jump, user login and operation status modify save password and JID, which I was put in the SDLoginVC controller, the SDLoginVC controller is the login interface. This controller is inherited from SDLaunchViewController, I write can do dynamic background page before, feeling in the shoes can click here to see in SDLaunchViewController. This controller is a timer, so when we jump page to the timer (rollTimer and timer) timely release.

/ / login success! Jump page (proxy Implementation) - (void) xmppStreamDidAuthenticate: (XMPPStream *) sender{XMPPPresence *presence [XMPPPresence presenceWithType:@ = "available"]; [[SDXmppManager defaulManager].stream sendElement:presence]; defaulUser].jid =[XMPPJID jidWithUser:self.userName.text domain:kDomin [SDUser resource:kResource] [SDUser; defaulUser].password = self.passWord.text; [[SDXmppManager defaulManager].roster fetchRoster]; if (self.timer! =nil) {[self.timer = invalidate]; self.timer nil}; if (self.rollTimer! = Nil) {[self.rollTimer} invalidate]; self.rollTimer = nil; self.view.window.rootViewController = self.contactNC;}

After a successful login, we can make the friends list information and personal information. This comes on the back of the blog. This is the basic process of login. Here we look at the implementation of the registration function.

The registration function of XMPPFramework is implemented

SDChat registration interface as follows, in fact, the user name is not used, such a registration interface in the user’s Avatar that part also appeared Bug. The next step to focus on.

XMPPFramework development (two): project configuration and login registration

The registration and login logic logic is basically the same, but also two parts, one part is connect to the server, the second part is to verify the registration password. The main code to login is consistent, is different. The password verification method method to self.stream registerWithPassword:self.regiserPassword error:& error];;

The main use of the registered – (void) xmppStreamDidRegister: (XMPPStream * sender) – xmppStream: (void) or failure (XMPPStream * sender) didNotRegister: (DDXMLElement *) error agent specific method is shown in the following code. We made the jump page in the method of successful registration, the user to remind the failure of the method in.

When the success of the registration / transfer - (void) xmppStreamDidRegister: (XMPPStream *) sender{UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:@ "successfully registered!" preferredStyle:UIAlertControllerStyleAlert]; __weak typeof (self) temp = self; UIAlertAction = *action [UIAlertAction actionWithTitle:@ style:UIAlertActionStyleDefault handler:^ ("return" UIAlertAction * _Nonnull action) {[temp popLoginVC];}]; [alertView addAction:action] [self presentViewController:alertView animated:YES; completion:nil];}
Called when the registration / / unsuccessful - (void) xmppStream: (XMPPStream * sender) didNotRegister: (DDXMLElement *) error{[self showAlertViewWithMessage:@ registration failed ~, please check the server "]};

After registration is completed, we can also set up personal business cards, etc. the latter will be in the personal electronic module, this section will be mentioned. Today, but no more explanation


Here basically XMPPFramework project configuration and register related knowledge is finished, the next blog will get to my friend XMPPFramework Sao, in the process of developing the list in detail. Finally the portal SDChat to everybody, you can also take a look at yourself, if you have any questions, welcome to contact our house, thank you.

–> SDChat portal

XMPPFramework development (two): project configuration and login registration