---
source_url: https://www.pubnub.com/docs/chat/community-supported/ios
title: Get started with PubNub Chat Components for iOS
updated_at: 2026-05-18T12:46:59.065Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Get started with PubNub Chat Components for iOS

:::warning Unsupported library
PubNub no longer supports this software library, but you are [welcome to contribute](https://github.com/pubnub/chat-components-ios).
:::

See how you can get a 1:1 chat app quickly up and running.

You will download a sample iOS app that uses two PubNub Chat Components for iOS: [MessageInput](https://www.pubnub.com/docs/chat/community-supported/ios/ui-components#messageinput) and [MessageList](https://www.pubnub.com/docs/chat/community-supported/ios/ui-components#messagelist). Then, you'll run the app and send your first message by typing it in the message input field. The messages will pile up on the screen as you send them.

## Prerequisites

* [Xcode](https://developer.apple.com/xcode/) (>= Xcode 13)
* [git](https://www.atlassian.com/git/tutorials/install-git)

:::note Tools used
This guide uses [PubNub Swift SDK](https://github.com/pubnub/swift) (>= 4.1.2) for chat components and [UIKit](https://developer.apple.com/documentation/uikit) as the UI Toolkit.
:::

## Steps

Follow the steps to get your PubNub keys, clone the sample iOS project files with chat components, and run the app to send your first message.

### Create a PubNub account

Before you start, you need to obtain [Publish and Subscribe Keys](https://www.pubnub.com/docs/general/basics/initialize-pubnub) for your chat app. You need them to initialize the PubNub object in your app to send and receive messages through the PubNub Network. To get both keys, sign in or create an [account](https://www.pubnub.com/docs/general/setup/account-setup) on the [Admin Portal](https://admin.pubnub.com/). The autogenerated Demo Keyset in My First App already contains the configuration required to complete this guide.

### Clone a sample chat app with components

Clone the [chat-components-ios-examples](https://github.com/pubnub/chat-components-ios-examples) repository.

```ssh
git clone git@github.com:pubnub/chat-components-ios-examples.git
```

If you're authenticated into Xcode with your GitHub account, you can also open the project directly from GitHub using the [Open in Xcode](https://github.blog/2017-06-05-clone-in-xcode/) button.

### Configure PubNub keys

1. Once you've cloned the repository, open the getting-started project in Xcode.
2. Inside SceneDelegate, replace both PUBNUB_PUBLISH_KEY and PUBNUB_SUBSCRIBE_KEY with your Publish and Subscribe Keys from your PubNub account in the Admin Portal: 1let PUBNUB_PUBLISH_KEY = "myPublishKey"2let PUBNUB_SUBSCRIBE_KEY = "mySubscribeKey"

The rest of the configuration is already provided:

* To associate a sender/current user with the PubNub messages, it's required to configure the userId parameter that stands for Universally Unique Identifier and refers to your user ID in the database. The app already sets userId in SceneDelegate using the pubnubConfiguration property. Both publishKey and subscribeKey parameters will use PUBNUB_PUBLISH_KEY and PUBNUB_SUBSCRIBE_KEY defined in the previous step. 1lazy var pubnubConfiguration = {2 return PubNubConfiguration(3 publishKey: PUBNUB_PUBLISH_KEY,4 subscribeKey: PUBNUB_SUBSCRIBE_KEY,5 userId: "myFirstUser"6 )7}() userIdFor simplicity, the getting started app sets a static userId. However, if you implement chat in your app, you should generate a userId per user, device, and server and reuse it for their lifetime. Using separate User IDs in real-life apps is particularly important as it impacts your overall billing and the way your app works.
* The app calls ChatProvider which initializes all the data components. These components are responsible for providing data to UI, setting the default theme, and communicating with the PubNub service. ChatProvider is used to facilitate the majority of the functionality provided by PubNub Chat Components for iOS. 1let provider = PubNubChatProvider(2 pubnubProvider: PubNub(configuration: pubnubConfiguration)3) Inside the scene(_:willConnectTo:options:) method of SceneDelegate, ChatProvider is initialized by passing in PubNubConfiguration. To learn more about this SceneDelegate method, see Apple's official documentation.
* As part of initialization, ChatProvider configures the default CoreData instance. In SceneDelegate, you can use the preloadData(_:) method to include any predefined data that should be added before using the chat components. 1func preloadData(_ chatProvider: PubNubChatProvider, completion: @escaping () -> Void) {2 // Creates a user object with userId3 let user = PubNubChatUser(4 id: chatProvider.pubnubConfig.userId,5 name: "myFirstUser",6 avatarURL: URL(string: "https://picsum.photos/seed/\(chatProvider.pubnubConfig.userId)/200")7 )8 9 // Creates a channel object10 let channel = PubNubChatChannel(11 id: defaultChannelId,12 name: "Default",13 type: "direct",14 avatarURL: URL(string: "https://picsum.photos/seed/\(defaultChannelId)/200")15 )16 17 // Creates a membership between the user and the channel for subscription purposes18 let membership = PubNubChatMember(channel: channel, user: user)19 20 // Subscribes to the default channel21 chatProvider.pubnubProvider.subscribe(.init(channels: [defaultChannelId], withPresence: true))22 23 // Fills the database with the user, channel, and memberships data24 chatProvider.dataProvider.load(members: [membership], completion: completion)25} ChatProvider can be stored at anytime using the corresponding load() methods for each of the object data types: load(channels:), load(users:), load(members:), load(messages:)
* The scene(_:willConnectTo:options:) method inside SceneDelegate finishes by loading the MessageList component as the default (root) View Controller. 1func setupRootView(windowScene: UIWindowScene) {2 // Creates the default ChannelList and MemberList component view models3 guard let channelListViewModel = chatProvider?.senderMembershipsChanneListComponentViewModel(),4 let messageListViewModel = try? chatProvider?.messageListComponentViewModel(pubnubChannelId: defaultChannelId) else {5 preconditionFailure("Could not create intial view models")6 }7 8 // Creates the navigation structure9 let navigation = UINavigationController()10 11 // Loads the MessageList as the root view, but allows for the ChannelList to be the previous view12 navigation.viewControllers = [13 channelListViewModel.configuredComponentView(),14 messageListViewModel.configuredComponentView()15 ]16 17 // Sets the component as the root view controller18 let window = UIWindow(windowScene: windowScene)19 window.rootViewController = navigation20 self.window = window21 window.makeKeyAndVisible()22} To make the application easier to use, the ChannelList component is also loaded as the previous View Controller in the app's navigation hierarchy. This is visualized by the Back button inside the navigation bar of the MessageList component that you will see once the app is up and running.

### Run the chat app

[Choose a simulator](https://developer.apple.com/documentation/xcode/running-your-app-in-simulator-or-on-a-device/) and run the `getting-started` app.

### Send your first message

When your application opens up, type and send your first message. The messages will pile up on the screen as you send them.

![Getting Started app for iOS](https://www.pubnub.com/assets/images/getting-started-ios-app-8346976698e43ce83e0904203c65fd06.png)

To verify that the app is connected to the PubNub Network, mock a real-live chat conversation with `myFirstUser`. Open the app in a new simulator, send a message from one simulator and reply to it from another.