Chat

Build a Fully-Featured iOS Chat App using Swift

4 min read Craig Lane on Apr 13, 2020
Swift-Hero-H.jpg

This article is old. New chat applications targeting iOS should use our new Swift Chat SDK, designed specifically to create chat applications on iOS. The Swift Chat SDK allows you to implement features like group / 1:1 messaging, threads, reactions, unread message indicators, & typing indicators quickly and easily.

Team Chat offers an example for how to implement PubNub Chat on Apple platforms. It works on iOS, iPadOS, and Mac OS, and includes features like user management, multiple group conversations, online/offline presence status, and more. Its UIs are built using SwiftUI, and it uses PubNub’s Client State Management framework to manage local state on the application.

The sample app, combined with our documentation, should serve as a resource for any developer looking to build in-app chat using our Swift SDK. It showcases how to go beyond the simple sending and receiving of messages and covers UI state management practices, which let you take full advantage of the features available in our chat API. In turn, this sample app can form the foundation for using PubNub chat in rich, production-ready applications.

The full GitHub repo for this project is available here.

SwiftUI makes it easy to build UI for light and dark modes.

About SwiftUI

The application uses SwiftUI: an innovative, simple way to build user interfaces across all Apple platforms with the power of Swift. SwiftUI uses a declarative syntax so you can simply state what your user interface should do. So, your code is simpler and easier to read than ever before. SwiftUI builds user interfaces for any Apple device using just one set of tools and APIs. With it, you can display your UI on any device and at any orientation. This lets you easily develop a flexible application that works well (and looks good) across iPhones, iPads, and Apple computers of all sizes. It also has prebuilt support for large fonts, localizations, left-handed mode, and dark mode, saving you time and maintenance.

About PubNub Client State Manager

PubNub Client State Manager is a framework that offers data management for apps built on the Apple platform. This framework listens to real-time events generated by the PubNub network to update the view inside your applications. And of course, your data can coexist with PubNub-defined data. It’s built upon ReSwift which is a Redux-like implementation of the unidirectional data flow architecture in Swift, and is quickly gaining popularity in the Swift community.

Why Client State Management

Have you ever worked on an unpredictable application? Perhaps the application works on the well-traveled "happy path." But, when users access less common features or combinations of features, bugs appear. And then when those bugs are fixed, more bugs are found or created. Or worse, you may have an unpredictable application in which bugs occur, but that can’t be replicated reliably, and don’t give evidence of their cause.

Modern user interfaces are complex. They feature complicated interactions, they're highly asynchronous, and they may be worked on by many different people. ReSwift attempts to solve these problems by placing strong constraints on the way applications can be written. This reduces the room for programmer error and leads to applications that can be more easily understood.

PubNub Client State Manager uses ReSwift to store data received via PubNub Messaging, connecting it to SwiftUI and helping to solve these problems of predictability and stability.

Run the app

Requirements

  • iOS 13.0+ / Mac Catalyst 13.0+ / tvOS 13.0+ / watchOS 6.0+

  • Xcode 11.4+

  • Swift 5+

PubNub Account

To run this application you must obtain publish and subscribe keys from your PubNub Account. If you don't already have an account, you can create one for free.

Get Chat Key from PubNub Portal

Sign in to your PubNub Dashboard.

  • Click Create New App.

  • Give your app a name, and select Chat App as the app type.

  • Select a region to store your user data (e.g. Portland). Click Create.

  • Click your new app to open its settings, then click its keyset.

  • Locate the Publish and Subscribe keys. You'll need these keys to include in this project.

Run the app

To run the application in your local environment, do the following:

1. Clone the GitHub repository.

2. Set the following environment variables inside your terminal's configuration file (such as ~/.bash_profile).

3. Setup the project.

4. When fastlane setup has completed, run the following command in your Terminal window:

5. Then, use the Run command on the TeamChat scheme to build and execute the application on your physical device or simulator.

Next Steps

Once you’ve gotten a chance to poke around our sample app, you may be interested in building something similar for your own use. Visit our docs for PubNub Chat to learn more about how to use SwiftUI and the PubNub Client State Management framework together, to add in-app chat to your applications. 

Additionally, PubNub Chat makes it super easy for developers to add rich chat features like unread message counts, typing indicators, read receipts, reactions and more. Check out our Chat Feature Guide, below, for more details.