In this tutorial, we’ll build a functional clone of iMessage using Stream Chat iOS SDK. Building a messaging app used to be difficult; but in this tutorial, you’ll get a chat experience up and running in roughly 20 minutes!

If you get lost during this tutorial, you can check:

The result of our application will look similar to the following screenshots:

Let’s get started! 🚀

Install Cocoapods

For this tutorial we are going to use CocoaPods as dependency manager. For convenience, we also publish the SDK on Cartage and Swift Package Manager.

If you do not currently have CocoaPods installed, or your cocoapods is outdated, you can update/install it by running the following command:

$ sudo gem install cocoapods

Cloning the iMessage Clone Starter Branch

Start by cloning the starter branch of the WhatsApp Clone Github repo:

$ git clone -b starter git@github.com:GetStream/stream-imessage-clone.git

and install dependencies:

$ pod install

After all of our pods are installed, you can open the Xcode workspace:

$ open iMessageClone.xcworkspace

The starter branch sets up the project and dependencies so we can jump right into coding! You can also create a new project; in that case, please make sure your Podfile is the same as that in the repo.

Configuring The Stream Chat Client

The first thing we need to do is configure our StreamChat Client with our API key. Open your AppDelegate file and edit your didFinishLaunchingWithOptions function, so it looks like this:

https://gist.github.com/b-onc/24055ae7edf3a71bde0242eb2ec76b10

Note: You can find your API key in the Stream Dashboard. To create an application, head over to https://getstream.io/chat/ and create a free account. Once created, click on the "Chat" tab within the dashboard. You will see that an application has been pre-provisioned for you. Within that application, you can find your credentials (API key and secret). After that, you can put your API secret and desired user id in this token generator to get the user token: https://getstream.io/chat/docs/token_generator/

Now that you've done that let's go ahead and move on.

Creating the Contacts Screen

We’ll start by creating a contacts (chats) view controller and make it look like that in the iMessage app. We’ll name it ContactsViewController .

Please create a new swift file and name it as shown above.

Note: If you’ve created a new project, rename your ViewController.swift and use it instead.

Then, paste these contents inside:

https://gist.github.com/b-onc/d3ac9ccf48109156643f5be4fd8c3e09

Then, open Main.storyboard and change the view controller class to ContactsViewController :

and embed the entry view controller inside a navigation controller:

After you’ve embedded a navigation controller, select your navigation item in a navigation controller and tick “Prefers Large Titles”:

Now to display the chats! Paste these lines into your ContactsViewController :

https://gist.github.com/b-onc/672d098fa660a1adbe6fceabb162b362

Now, if you run the app, you’ll see that our contacts screen already looks a lot like iMessage, and we have only just used the customization options provided by Stream Chat:

Our contacts cells lack the blue dot showing the unread status of a chat, the chevron symbol next to the dates, and the date texts are not formatted to look like iMessage. Let’s fix that!

For these kinds of things for which Stream Chat does not provide an API, we can subclass Stream Chat’s classes and add our functionality. Let’s do that!

Create a new file named ContactListCell.swift and paste these contents:

https://gist.github.com/b-onc/fd221721617fa70e5c79d34fcf2758e2

We’ve extended Stream Chat’s ChannelTableViewCell to add our components (unread indicator and date accessory), and we’ve overridden update(date:) function to display our date string.

To be able to use our new cell, we need to add some code inside ContactsViewController .

Inside viewDidLoad , before tableView.tableFooterView = nil line:

https://gist.github.com/b-onc/037b3cb097e1e4335fa3e1c1e0ad133e

and paste this new function:

https://gist.github.com/b-onc/d9a230bb24a668aea1af8bae484adf7b

You only need to register your cell type, dequeuing is done automatically by our SDK and overriding updateChannelCell is enough for customizing your cell.

Now if you run it, you’ll see that we were able to implement the missing features:

Great!

Building the Messaging Screen

Now, we move onto the Messaging screen. Currently, we haven’t touched it yet, so it looks nothing like iMessage:

Let’s start customizing our screen!

Paste this code inside ContactsViewController 's setupStyles function:

https://gist.github.com/b-onc/bfeb6c504e381e8b8fe737393e3c806f

If you run the app now, you’ll see colors and message bubbles look like those of iMessage, but the navigation bar title is buggy, as it displays a significantly larger title. In addition, we are missing some UI buttons that iMessage has built-in.

To fix the “large title situation” (and implement our custom navigation item title), we have to subclass Stream Chat’s ChatViewController .

Create a new swift file and name it MessagesViewController.swift . Then, paste in the contents:

https://gist.github.com/b-onc/5778255826b21cd6930e3556bb2b7cde

We’ve added a new class ChatNavigationTitleView to imitate iMessage’s title view. It’s not the same, since iOS restricts our access to the navigation bar, but it’s close enough! We could’ve used a custom navigation bar to imitate 1-1, but it’d be another tutorial, then…

To use our new MessagesViewController , we need to add this into our ContactsViewController :

https://gist.github.com/b-onc/528a8ea0d5a79d1f248366a99316e6b8

This completes our work in ContactsViewController 🎉 If you run the app now, you'll see that our messages screen looks a lot more like iMessage now. But we still have some UI buttons to add to make it look more like iMessage. Let's add them now.

Add these in your MessagesViewController as properties:

https://gist.github.com/b-onc/4df8a6f7d68f2253151d529906b082bd

Then, add the setupUI function:

https://gist.github.com/b-onc/4416132a0ec4ebe1c1791b451500f36e

Lastly, add this call to your viewDidLoad (as the last call):

https://gist.github.com/b-onc/3a3283e37de22a91e8948447f9ce4deb

and now run the app:

Wrapping Up!

We have now completed the messaging section of our iMessage imitator, using Stream Chat’s API! If you want to take this further, make sure to check the Stream docs to find out all the features that are available to you.

This concludes our tutorial, happy coding! 🎉