Thursday, January 23, 2014

Objective c methods for new user registration in xmpp framework

For chat applications, users have to register by creating their account or using social networking accounts like Facebook or Twitter. This registration process can be done by using normal web services running on our servers and saving the details in the database. But for this, we need web services for registration and database to save the user's registration details along with you will have to care of many scenarios like invalid user names, email etc at the time registration process. So it's definitely a time taking and tedious approach to own the XMPP registration process.


The good news here is, if you are using XMPP as your char server, you are then on safe hands. Why because XMPP creates it's own database also at the time of XMPP server installation and also the iOS XMPP framework provides you the methods for user registration which is vey simple when compared to our custom .net/PHP/Java web services that we need to write for registration process.

Here is the sample Objective C code for XMPP user registration.

NSMutableArray *ma = [NSMutableArray array];
[ma addObject:[NSXMLElement elementWithName:@"username" stringValue:@"john"]];
[ma addObject:[NSXMLElement elementWithName:@"password" stringValue:@"john123"]];
[ma addObject:[NSXMLElement elementWithName:@"name" stringValue:@"John Paul"]];
[ma addObject:[NSXMLElement elementWithName:@"accountType" stringValue:@"3"]];
[ma addObject:[NSXMLElement elementWithName:@"deviceToken" stringValue:@"devicetoken123"]];
[ma addObject:[NSXMLElement elementWithName:@"email" stringValue:@""]];

[[[self appDelegate] xmppStream] registerWithElements:ma error:nil];

XMPPStream object has a method called registerWithElemnets, which takes an array of xml elements like username, password, email etc. This approach of using builtin XMPP method for registration is simple and the most important thing is this service is very fast in speed or performance compared to our custom web services. And also it takes care of the scenarios like 'username already exists' etc. So it's simple but powerful approach to follow for user registration in a iOS chat application using Objective-c.

Now, we are able to register the user using the XMPP registerWithElemnets method. Though it's simple, we need to know whether the registration process is successful or not and if not what are the issues that failed registration process. XMPP has delegate methods for successful registration and also for failed registration. 

  • The delegate method for successful registration process is xmppStreamDidRegister. If this delegate callback method is called your XMPP user registration process is successful. You can proceed use to your application.

Here is the Objective C code for xmppStreamDidRegister delegate method.

- (void)xmppStreamDidRegister:(XMPPStream *)sender{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration" 
                                   message:@"Registration Successful!" delegate:nil cancelButtonTitle:@"OK" 
                                   otherButtonTitles:nil, nil];
    [alert show];

  • The delegate method for unsuccessful registration process is didNotRegister. If this delegate callback method is called your XMPP user registration process is failed. This method has a error parameter, using which you can know the XMPP error code and error description and alert the user about this error.


Here is the Objective C code for didNotRegister delegate method.

- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error{
    DDXMLElement *errorXML = [error elementForName:@"error"];
    NSString *errorCode  = [[errorXML attributeForName:@"code"] stringValue];   

    NSString *regError = [NSString stringWithFormat:@"ERROR :- %@",error.description];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration Failed!" 
                        message:regError delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    if([errorCode isEqualToString:@"409"]){        

        [alert setMessage:@"Username Already Exists!"]; 

You can alert the user based on the error code as shown in the above code.

Feel free to comment in case of any queries or concerns.