Wednesday, February 17, 2016

Objective C XMPP framework User Registration Methods

Using XMPPStream class registerWithElements method, we can register users to Openfire server using XMPP framework.

Here is the piece of code to prepare registration elements and calling registration method in Objective C.




registerWithElements:




 NSMutableArray *elements = [NSMutableArray array];


 [elements addObject:[NSXMLElement elementWithName:@"username" stringValue:@"karun"]];

 [elements addObject:[NSXMLElement elementWithName:@"password" stringValue:@"pwd"]];

 [elements addObject:[NSXMLElement elementWithName:@"name" stringValue:@"Karunakar"]];

 [elements addObject:[NSXMLElement elementWithName:@"accountType" stringValue:@"3"]];

 [elements addObject:[NSXMLElement elementWithName:@"deviceToken" stringValue:@"1234"]];

 [elements addObject:[NSXMLElement elementWithName:@"email" stringValue:@"ios@solves.com"]];


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






Of course, we need to know the status of registration service call. We can check the status using the below delegate methods.


xmppStreamDidRegister:




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

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

    [alert show];

 }



didNotRegister:




 - (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error{

    DDXMLElement *errorXML = [error elementForName:@"error"];

    NSString *errorCode  = [[errorXML attributeForName:@"code"] stringValue];   

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

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

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

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

    [alert show];


 }



Hope this post is useful. Fell free to comment in case of any queries.