Friday, August 28, 2015

Objective c XMPP search users by username

In chat applications, we obviously need to have 'Add Friend' functionality without which there is no meaning of it calling a chat application as there will be no real fun in chatting with yourself :)

Some chat applications provide users an option to directly making them friends with their contacts, Facebook friends, Twitter followers etc. That is how chat applications make app users life easy by directly fetching and giving their contacts and social networking friends.


Any how, our chat app users need an option to search for users based on their name, email etc. In this article I am going to show, how I have done this functionality in XMPP framework using Objective c  for iOS.

Objective c XMPP Search Query : 
NSString *userBare1  = [[[[self appDelegate] xmppStream] myJID] bare];    

NSXMLElement *query = [NSXMLElement elementWithName:@"query"];
[query addAttributeWithName:@"xmlns" stringValue:@"jabber:iq:search"];

NSXMLElement *x = [NSXMLElement elementWithName:@"x" xmlns:@"jabber:x:data"];
[x addAttributeWithName:@"type" stringValue:@"submit"];

NSXMLElement *formType = [NSXMLElement elementWithName:@"field"];
[formType addAttributeWithName:@"type" stringValue:@"hidden"];
[formType addAttributeWithName:@"var" stringValue:@"FORM_TYPE"];
[formType addChild:[NSXMLElement elementWithName:@"value" stringValue:@"jabber:iq:search" ]];

NSXMLElement *userName = [NSXMLElement elementWithName:@"field"];
[userName addAttributeWithName:@"var" stringValue:@"Username"];
[userName addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1" ]];

NSXMLElement *name = [NSXMLElement elementWithName:@"field"];
[name addAttributeWithName:@"var" stringValue:@"Name"];
[name addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];

NSXMLElement *email = [NSXMLElement elementWithName:@"field"];
[email addAttributeWithName:@"var" stringValue:@"Email"];
[email addChild:[NSXMLElement elementWithName:@"value" stringValue:@"1"]];

NSXMLElement *search = [NSXMLElement elementWithName:@"field"];
[search addAttributeWithName:@"var" stringValue:@"search"];
[search addChild:[NSXMLElement elementWithName:@"value" stringValue:@""]];

[x addChild:formType];
[x addChild:userName];

[x addChild:search];
[query addChild:x];

NSXMLElement *iq = [NSXMLElement elementWithName:@"iq"];
[iq addAttributeWithName:@"type" stringValue:@"set"];
[iq addAttributeWithName:@"id" stringValue:@"searchByUserName"];
[iq addAttributeWithName:@"to" stringValue:[NSString stringWithFormat:@"search.%@",[self appDelegate].hostName ]];
[iq addAttributeWithName:@"from" stringValue:userBare1];
[iq addChild:query];
[[[self appDelegate] xmppStream] sendElement:iq];

In this, I am preparing a jabber search query xml element. The namespace is I am preparing the below fields for search query.

  • Username
  • Name
  • Email


That means, I want a user whose username or Name or Email contains my search string. If you don't need any of the above fields, you can remove it in the query. At the end, I am passing the search string value that we are looking for. We need to send this request to search. url. 

For this all to work, We need to enable search option in the XMPP server admin panel. Otherwise it won't workout. In the admin panel you will be having options like enabling only username search or only email search etc.

Hope this article is useful to iOS chat application developers using XMPP.
Feel free to comment in case of any queries/concerns.

No comments:

Post a Comment