Using the PubNub Swift Chat SDK

PubNub's Swift Chat SDK is the recommended way for iOS developers to create new chat applications or add in-app chat to existing applications

Step 01

Set up environment

This tutorial walks you through setting up a basic sample app created using the PubNub Swift Chat SDK. It explains the app architecture and the code behind the main chat features.


The Swift Chat SDK sample used in this tutorial is available at https://github.com/PubNubDevelopers/Chat-SDK-Tutorial-Swift and is released under MIT. The sample is designed for illustrative purposes only and has minimal error handling.


The sample targets PubNub Swift Chat SDK 1.0.0.


The Swift Chat SDK is UI and framework agnostic, allowing you complete control over how your application looks and how it is architected. The sample application has been written with SwiftUI and some custom styling.



Prerequisites

Make sure you have the following installed on your computer:

Download the source application

In a terminal or command window, navigate to an empty folder. Enter the following command to clone the repository.

Code
git clone https://github.com/PubNubDevelopers/Chat-SDK-Tutorial-Swift.git


Open the Chat-SDK-Tutorial-Swift/PubNubSwiftChatSDKTutorial folder in Xcode. Xcode should automatically download the required package dependencies, including PubNub Swift Chat SDK 1.0.0.


If package resolution does not start automatically, use File -> Packages -> Resolve Package Versions in Xcode.


The sample stores your PubNub keys in this file:

/PubNubSwiftChatSDKTutorial/PubNubSwiftChatSDKTutorial/Keys.swift

The file currently contains placeholders for your PubNub publish and subscribe keys:

Code
// REPLACE THESE WITH YOUR PUB / SUB KEYS
struct Keys {
    static let PUBNUB_PUBLISH_KEY="YOUR_PUBLISH_KEY_HERE"
    static let PUBNUB_SUBSCRIBE_KEY="YOUR_SUBSCRIBE_KEY_HERE"
}

The next step is to generate your own publish and subscribe keys to replace these placeholders.

Step 02

Create keys

Demo keys

You're ready to go!

We’ve assigned demo keys which are set up to support the features of this tutorial app. These will be active for 72 hours, but you can sign up for a free account at the end of the tutorial to create your own permanent keys.

The demo keys have the following functionality activated ready for your application:

  • App Context this allows you to manage the metadata of your users and channels
  • Presence you will be able to track user online and offline status
  • Message Persistence You will be able to see user message history and metadata.


My own keys

To use your own keys, create a free account and a keyset with App Context, Presence, and Message Persistence on.

You can create the keyset with the Admin API, PubNub's REST API for account management. An Account Owner or Account Admin creates a Service Integration and its API key in the Admin Portal under Organization Settings → API Management. Give it read & write access to apps and keysets, plus read access to secret keys, which keyset creation requires. With that key, create an app, then create a keyset in it and pass the keyset's configuration in the same request. The response includes the publish and subscribe keys. See the Admin API documentation for the requests and keyset settings.

Step 03

Build and run

Step 04

Congratulations - what's next?

Chat SDK Documentation

Understand the Source Code

This tutorial has only scratched the surface of the capabilities of our Swift Chat SDK, we are continually adding new features, so check out the source code, available at https://github.com/pubnub/swift-chat-sdk

The source code for the sample app discussed in this tutorial is available from https://github.com/PubNubDevelopers/Chat-SDK-Tutorial-Swift

Chat SDK Demos in other Languages

Experience our fully-featured group chat app for the web, written in TypeScript and Next.js:

Tutorial - Chat SDK - React Chat Demo

Ready to get beyond Proof of Concept?

Schedule a 15 minute chat with one of our experts to get your project moving

Talk to an Expert

Got a question for our developer relations team? Email devrel@pubnub.com

Keep building

Take it to production on your own keys

Create a free PubNub account to get permanent keys for this app.