If you want to add chat functionality in your iOS app . Then this blog can help you to do some basic chat functionality. Such as creating xmpp session , login on sever, creating room, getting list of login buddies and chatting.
First step download the XMPP framework from the following link :
https://github.com/robbiehanson/XMPPFramework
call this function to start and setup xmpp stream 
    -(void)startXMPP:(id)viewController{
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
    // Setup the XMPP stream
    NSLog(@"XMPP new method called");
      self.delegate=viewController;
    [self setupStream];
    if (![self connect])
    {
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.0 * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        });
    }
}
- (void)setupStream
{
    NSAssert(xmppStream == nil, @"Method setupStream invoked multiple times");
    // Setup xmpp stream
    //
    // The XMPPStream is the base class for all activity.
    // Everything else plugs into the xmppStream, such as modules/extensions and delegates.
    xmppStream = [[XMPPStream alloc] init];
#if !TARGET_IPHONE_SIMULATOR
    {
        // Want xmpp to run in the background?
        //
        // P.S. - The simulator doesn't support backgrounding yet.
        //        When you try to set the associated property on the simulator, it simply fails.
        //        And when you background an app on the simulator,
        //        it just queues network traffic til the app is foregrounded again.
        //        We are patiently waiting for a fix from Apple.
        //        If you do enableBackgroundingOnSocket on the simulator,
        //        you will simply see an error message from the xmpp stack when it fails to set the property.
        xmppStream.enableBackgroundingOnSocket = YES;
    }
#endif
    // Setup reconnect
    //
    // The XMPPReconnect module monitors for "accidental disconnections" and
    // automatically reconnects the stream for you.
    // There's a bunch more information in the XMPPReconnect header file.
    xmppReconnect = [[XMPPReconnect alloc] init];
    // Setup roster
    //
    // The XMPPRoster handles the xmpp protocol stuff related to the roster.
    // The storage for the roster is abstracted.
    // So you can use any storage mechanism you want.
    // You can store it all in memory, or use core data and store it on disk, or use core data with an in-memory store,
    // or setup your own using raw SQLite, or create your own storage mechanism.
    // You can do it however you like! It's your application.
    // But you do need to provide the roster with some storage facility.
    xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
    //    xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] initWithInMemoryStore];
    xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:xmppRosterStorage];
    xmppRoster.autoFetchRoster = YES;
    xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES;
    // Setup vCard support
    //
    // The vCard Avatar module works in conjuction with the standard vCard Temp module to download user avatars.
    // The XMPPRoster will automatically integrate with XMPPvCardAvatarModule to cache roster photos in the roster.
    xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
    xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];
    xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:xmppvCardTempModule];
    // Setup capabilities
    //
    // The XMPPCapabilities module handles all the complex hashing of the caps protocol (XEP-0115).
    // Basically, when other clients broadcast their presence on the network
    // they include information about what capabilities their client supports (audio, video, file transfer, etc).
    // But as you can imagine, this list starts to get pretty big.
    // This is where the hashing stuff comes into play.
    // Most people running the same version of the same client are going to have the same list of capabilities.
    // So the protocol defines a standardized way to hash the list of capabilities.
    // Clients then broadcast the tiny hash instead of the big list.
    // The XMPPCapabilities protocol automatically handles figuring out what these hashes mean,
    // and also persistently storing the hashes so lookups aren't needed in the future.
    //
    // Similarly to the roster, the storage of the module is abstracted.
    // You are strongly encouraged to persist caps information across sessions.
    //
    // The XMPPCapabilitiesCoreDataStorage is an ideal solution.
    // It can also be shared amongst multiple streams to further reduce hash lookups.
    xmppCapabilitiesStorage = [XMPPCapabilitiesCoreDataStorage sharedInstance];
    xmppCapabilities = [[XMPPCapabilities alloc] initWithCapabilitiesStorage:xmppCapabilitiesStorage];
    xmppCapabilities.autoFetchHashedCapabilities = YES;
    xmppCapabilities.autoFetchNonHashedCapabilities = NO;
    // Activate xmpp modules
    [xmppReconnect         activate:xmppStream];
    [xmppRoster            activate:xmppStream];
    [xmppvCardTempModule   activate:xmppStream];
    [xmppvCardAvatarModule activate:xmppStream];
    [xmppCapabilities      activate:xmppStream];
    // Add ourself as a delegate to anything we may be interested in
    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()];
    // Optional:
    //
    // Replace me with the proper domain and port.
    // The example below is setup for a typical google talk account.
    //
    // If you don't supply a hostName, then it will be automatically resolved using the JID (below).
    // For example, if you supply a JID like 'user@quack.com/rsrc'
    // then the xmpp framework will follow the xmpp specification, and do a SRV lookup for quack.com.
    //
    // If you don't specify a hostPort, then the default (5222) will be used.
    //    [xmppStream setHostName:@"talk.google.com"];
    //    [xmppStream setHostPort:5222];
    // You may need to alter these settings depending on the server you're connecting to
    allowSelfSignedCertificates = NO;
    allowSSLHostNameMismatch = NO;
}
After setting up xmpp stream , login using your user email and password by following method:
- (void)goOnline
{
    XMPPPresence *presence = [XMPPPresence presence]; // type="available" is implicit
    NSString *domain = [xmppStream.myJID domain];
    //Google set their presence priority to 24, so we do the same to be compatible.
    if([domain isEqualToString:@"gmail.com"]
       || [domain isEqualToString:@"gtalk.com"]
       || [domain isEqualToString:@"talk.google.com"])
    {
        NSXMLElement *priority = [NSXMLElement elementWithName:@"priority" stringValue:@"24"];
        [presence addChild:priority];
    }
    [[self xmppStream] sendElement:presence];
}
To create the room , use this method :
-(void)createRoom
{
    NSLog(@"creating room");
    XMPPJID *roomJID = [XMPPJID jidWithString:@"jitu@conference.umekir.com"];
    XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
    XMPPRoom *xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage
                                                           jid:roomJID
                                                 dispatchQueue:dispatch_get_main_queue()];
    [xmppRoom activate:self.xmppStream];
    [xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [xmppRoom joinRoomUsingNickname:[[NSUserDefaults standardUserDefaults] stringForKey:@"email"] history:nil];
}
Following are delegates method of XMPPRoomDelegate
- (void)xmppRoomDidCreate:(XMPPRoom *)sender{
    NSLog(@"able to create the room==%@",sender);
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
- (void)xmppRoomDidJoin:(XMPPRoom *)sender{
    NSLog(@"able to join the room==%@",sender);
    DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}
Following method will give the status of online or offline user. As the user will get online/offline following method will called up
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence
{
    NSLog(@"notfication for presence");
    NSString *presenceType = [presence type];
    NSLog(@"presence type==%@",presenceType);// online/offline
    [self parsingpresence:[presence fromStr] :presenceType];
}
-(void)parsingpresence:(NSString*)emailidwithRoom :(NSString*)status
{
    NSArray *tmp=[emailidwithRoom componentsSeparatedByString:@"/"];
    if(tmp.count>0)
    {
        NSString *emailid=[tmp objectAtIndex:1];
        NSLog(@"emailid==%@",emailid);
        NSUserDefaults *usrdef=[NSUserDefaults standardUserDefaults];
        [usrdef valueForKey:@"kXMPPmyJID"];
        if(![emailid isEqualToString:[usrdef valueForKey:@"kXMPPmyJID"]] && [status isEqualToString:@"available"]){
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ReadPresenceNotification" object:emailid];
        } else {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ReadRemoveNotification" object:emailid];
        }
    }
}
To send the message to buddy, pass buddy email id to the following function and message 
- (void)sendChatMessage :(NSString*)messageStr :(NSString*)frndemailid{
   // NSString *messageStr = @"hello how r u";
    if([messageStr length] > 0) {
        NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
        [body setStringValue:messageStr];
        NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
        [message addAttributeWithName:@"type" stringValue:@"chat"];
        [message addAttributeWithName:@"to" stringValue:frndemailid];
        [message addChild:body];
        [self.xmppStream sendElement:message];
    }
}
I have attached the source code of sample application . Enjoy chatting  :) 
                       
                    
0 Comment(s)