Tuesday, June 14, 2016

XMPP user change password using Objective C

For changing logged in user's password, we need to query jabber:iq:register along with the username and new password of the user.

Here is the Objective-C code to send this password change query stream element.




    DDXMLElement *iq = [DDXMLElement elementWithName:@"iq"];
    [iq addAttributeWithName:@"type" stringValue:@"set"];
    [iq addAttributeWithName:@"to" stringValue:[self appDelegate].hostName];
    [iq addAttributeWithName:@"id" stringValue:@"changepassword"];
    
    DDXMLElement *query = [DDXMLElement elementWithName:@"query"];
    [query addAttributeWithName:@"xmlns" stringValue:@"jabber:iq:register"];
    
    DDXMLElement *username = [DDXMLElement elementWithName:@"username"];
    [username setStringValue:userName];
    [query addChild:username];
    
    DDXMLElement *password = [DDXMLElement elementWithName:@"password"];
    [password setStringValue:newPass];
    [query addChild:password];
    
    [iq addChild:query];
    
    
    [[[self appDelegatexmppStreamsendElement:iq];



'chanepassowrd' is my custom ID I have given for this query. So that in the didReceiveIQ delegate method, I can check with this ID for password change.




  - (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq{


        NSString *iqID  =  [[iq attributeForName:@"id"stringValue];
    
        if([iqID isEqualToString:@"changepassword"]){

            //Password change is successful!
        }


  }




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