Search code examples
iosobjective-cxmppxmppframework

Setting up XMPPFramework for iOS


I am working on an iOS app with a chat function. I want to know if there is any resource to configure the XMPPFramework in order to connect my iOS app with Openfire server.

I am new to XMPP Protocol.

I am currently learning about XMPP Stream and Rosters but I need to at least get the connection to work.

Please help.


Solution

  • To get yourself started, dive into the sample iOS project in XMPPFramework-master > Xcode > iPhoneXMPP.

    Preferably, begin tweaking in the project itself, and get your understanding from there before moving on to create your own XMPP project.

    Basically to connect XMPP to OpenFire server, most of the configurations lie in AppDelegate.

    1. Set your OpenFire server's details in the XMPP setup:

      - (void)setupStream
      {
          ...
      
          // Specify your server's IP address
          [xmppStream setHostName:@"123.12.123.12"];
      
          // Specify your host port
          [xmppStream setHostPort:5222];
      }
      
    2. Assuming you have already created a contact in your OpenFire's roster, set a contact's credentials in the XMPP connection method:

      - (BOOL)connect
      {
          /**
           * Of course, do not hardcode in an actual implementation
           * Appending the server name at the back of user ID is necessary
           */
          myJID = @"user@openfire";
          myPassword = @"password goes here";
      }
      
    3. Ensure you call the connect method in app launch method:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          [self connect];
      }
      
    4. Ensure you are connected here:

      - (void)xmppStreamDidConnect:(XMPPStream *)sender
      {
          NSLog(@"User Connected");
          // You are connected to the server at this point.            
      }
      
    5. Ensure you are authenticated here:

      - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender
      {
          NSLog(@"User Authenticated");
          /**
           * Once you've reached this point,
           * Check your server for the online users. 
           * You should now be seen as "available".
           * Cheers!
           */
      }