This tutorial shows you how to build a fully-featured iOS group messaging chat application with Objective-C and PubNub.
Designing your first app on a new platform is always fun, especially when it deals with an area you’re familiar with. You get to experiment with different programming features, understand different device capabilities, and maybe even learn a new language.
I decided to develop a group chat client, because I felt that while there are many simple peer-to-peer chat clients, there are no dedicated client that helps large groups communicate in a fast and efficient way. As a result, PubNub iOS Group Messenger was born. Want to get started right away? All the code is available in the GitHub Repository.
**NOTE: Since we wrote this tutorial, we’ve released a new, completely-redesigned version of our iOS SDK (4.0). We rebuilt the entire codebase and took advantage of new features from Apple, simplifying and streamlining the SDK.
The code from this tutorial still works great, but we recommend checking out our new SDK. We’ve included a migration guide to help you move from 3.x to 4.0, and a getting started guide to help you create a simple Hello World application in minutes.**
Using Objective-C for iOS Group Messaging
In the beginning, it was an uphill struggle. XCode is notoriously hard to code with and Objective-C definitely has its idiosyncrasies. Formally implementing all the code for each UI feature can get a bit cumbersome, especially for an app composed of many different parts. For someone coming from a web-development background, Objective-C can seem clunky and verbose.
However, designing an iPhone app is a fun experience simplified heavily by the use of the model-view-controller framework and storyboards. Adding viewcontrollers, search bar controllers and the like was super easy, even if deleting them was a pain (the annoying key value coding compliant comes to mind).
I had seven different screens for my app (a few can be seen throughout this blog post). There are many different features implemented: friendly bubble-like boxes for messages, and icons to indicate to the user what to do next for example. Want to see it in action? Check out the two minute demo below:
One of the main aims of PubNub iOS Group Messenger is to make the messaging experience as self explanatory as possible. Without pointing fingers at anyone in particular, I’ve noticed some chat clients take a bit getting used to.
Users simply input their name and phone number and are good to go. The number is similar to a public-key; in other words, as long as the user’s number and name is known to the other party, messages can be sent. The layout of the app is centred around a page with all the groups listed. It’s the first thing a user sees and contains buttons to add and delete groups. The simplicity of the interface really drives the app and its self explanatory design.
Messaging just involves sending a message; the name of the sender is automatically recorded and sent to all those who are meant to receive it. The user can easily add and delete members using the view/edit button.
Adding and Deleting Users in iOS Group Messaging
An important feature of iOS Group Messenger is that the user can decide who he wants to receive messages from. Users can easily select a person and drop him from a group at the touch of a button. The other party will not be alerted of which group he or she is included in. It also allows other members of the group to get the person’s messages. It also allows you to remain in the group as well.
PubNub and iOS
While the UI might draw a person to the app, the core of iOS Group Messenger involves sending messages and receiving them. That’s where the PubNub Data Stream Network comes in. PubNub provides an easy-to-use framework that allows users to send and receive messages seamlessly. All you need is a developer account and the PubNub libraries in order to send and receive messages on channels specified by the app itself. I simply dropped the folders in (shown below):
I then added a
didReceiveMessage function in my
AppDelegate.m file and followed the tutorials. I sent a complex message after identifying the sender’s country of origin, appended that number to the user’s and then sent the message along with it.
One of the benefits of using PubNub is that it allows you to easily implement the selective-listening feature. Just by configuring channels, you can easily allow the user the freedom to choose the members of his group.
Once you create the basic app, there’s no limit to the number of features you can add. For example, use the app to support bulk email messaging using separate libraries or integrate app into a game to communicate with your teammates.
Here’s a couple code samples to give you an idea of what went into developing the app. As always, all the code is available in the GitHub Repository.
Here I configured the app to receive messages:
And here I sent messages, in the above-mentioned specialized format.