Getting Started
Start your first conversation
CometChat UI Kit for iOS is a package of pre-assembled UI elements crafted to streamline the creation of an in-app chat equipped with essential messaging functionalities. Our UI Kit presents options for light and dark themes, diverse fonts, colors, and extra customization capabilities.
CometChat UI Kit supports both one-to-one and group conversations. Follow the guide below to initiate conversations from scratch using CometChat iOS UI Kit.

Prerequisites
Before installing the UI Kit, you need to create a CometChat application on the CometChat Dashboard, which includes all the necessary data for a chat service, such as users, groups, calls, and messages. You will require the App ID
, AuthKey
, and Region
of your CometChat application when initializing the SDK.
i. Register on CometChat
- You need to register on the CometChat Dashboard first. Click here to sign up.
ii. Get Your Application Keys
- Create a new app
- Head over to the QuickStart or API & Auth Keys section and note the App ID, Auth Key, and Region.
Each CometChat application can be integrated with a single client app. Within the same application, users can communicate with each other across all platforms, whether they are on mobile devices or on the web.
iii. IDE Setup
- You have Xcode installed on your machine.
- You have an iOS device or simulator with iOS version 13.0 or above.
- Swift 5.0.
- macOS.
Getting Started
Step 1Create a project
To get started, open Xcode and create a new project for UI Kit in the Project window as follows:
- Select iOS App in the Choose a template for your new project window and click Next.
- Enter your project name in the Name field in the Choose options for your new project window.
- Enter your identifier in the Bundle Identifier field in the Choose options for your new project window.
- Select Storyboard in the Interface field in the Choose options for your new project window.
- Select Swift in the Language field in the Choose options for your new project window.
Step 2
Add Dependency
This developer kit is an add-on feature to CometChat iOS SDK so installing it will also install the core Chat SDK. You can install add CometChatUIKit into you iOS project using CocoaPods and Swift Package Manager (SPM).
We recommend using CocoaPods, as they are the most advanced way of managing iOS project dependencies. Open a terminal window, move to your project directory, and then create a Podfile by running the following command.
- CocoaPods
- Swift Package Manager (SPM)
Create pod file by running following command into you project's base level
$ pod init
Add the following lines to the Podfile.
platform :ios, '13.0'
use_frameworks!
target 'YourApp' do
pod 'CometChatUIKitSwift', '5.0.1'
end
And then install the CometChatUIKit framework through CocoaPods.
$ pod install
If you're facing any issues while installing pods then use the below command.
$ pod install --repo-update
Always get the latest version of CometChatUIKit by command.
$ pod update CometChatUIKitSwift
Always ensure to open the XCFramework file after adding the dependencies.
Swift Package Manager (SPM) is Apple’s built-in tool for managing dependencies in Swift projects. It allows developers to integrate and manage third-party libraries seamlessly.
-
Go to File tab and select Add Package Dependencies.
-
Enter the repository URL of the Swift package.
https://github.com/cometchat/cometchat-uikit-ios
-
To add the package, select Version Rules, enter Up to Exact Version and click Add package.
Exact Version
5.0.1
-
Add
CometChatSDK
repeating the above steps for following link and exact version:Link
https://github.com/cometchat/chat-sdk-ios.git
Exact Version
4.0.55
Step 3
Configure Privacy Permissions
-
To enable media messaging in your app, you must allow Camera, Microphone, and Photo Library access in
Info.plist
. These permissions are required for sending and receiving media files.Info.plist<key>NSCameraUsageDescription</key>
<string>Allow access to the camera to capture photos and videos.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Enable microphone access for voice and video communication.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Allow saving photos and videos to your device's photo library.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Grant access to select and upload photos from your library.</string> -
Navigate to your Build Settings and disable the User Script Sandboxing option.
Disabling User Script Sandboxing ensures that WebView can load and execute scripts necessary for collaborative tools.
Step 4
Initialize & Login
To start using the UI Kit, you first need to initialize
and log in
. Follow the steps below to get started:
-
Initialization
Call theInit
method to set up the necessary configurations for CometChat in your app. This step should be done before invoking any other methods from CometChat UI Kit or SDK. -
User Login
Once initialized, use thelogin()
method to authenticate the user. This method will return a User object containing all relevant user details. For testing purposes, you can use the following pre-generated users:cometchat-uid-1
cometchat-uid-2
cometchat-uid-3
cometchat-uid-4
cometchat-uid-5
For more details, refer to the documentation on Init and Login.
You can initialize CometChat and log in a user in your SceneDelegate.swift
file. Upon successful login.
import UIKit
import CometChatUIKitSwift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let uikitSettings = UIKitSettings()
.set(appID: "<#Enter Your App ID Here#>")
.set(authKey: "<#Enter Your AuthKey Here#>")
.set(region: "<#Enter Your Region Code Here#>")
.subscribePresenceForAllUsers()
.build()
CometChatUIKit.init(uiKitSettings: uikitSettings) { result in
switch result {
case .success:
debugPrint("CometChatUIKit initialization succeeded")
let uid = "cometchat-uid-1"
CometChatUIKit.login(uid: uid) { loginResult in
switch loginResult {
case .success:
debugPrint("CometChatUIKit login succeeded")
case .onError(let error):
debugPrint("CometChatUIKit login failed with error: \(error.description)")
@unknown default:
break
}
}
case .failure(let error):
debugPrint("CometChatUIKit initialization failed with error: \(error.localizedDescription)")
}
}
}
}
Make sure to replace the appId with your CometChat appId and region with your app region in the above code.
After running the app, you should see the following log message:
"CometChatUIKit initialization succeeded"
"CometChatUIKit login succeeded"
Render Conversations
CometChatConversations is a component that enables opening the MessagesVC(your custom view controller to show chat messages) by tapping on any conversation rendered in the list of conversations.
This code sets up CometChatConversations
as the main screen inside a navigation controller, making it the entry point of the app. After initializing the conversations view, it wraps it in a UINavigationController, sets it as the root view controller, and ensures the window is visible.
let conversationsVC = CometChatConversations()
let navController = UINavigationController(rootViewController: conversationsVC)
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
The final SceneDelegate.swift
file includes both CometChat initialization and navigation to the Conversations screen and Conversation onItemClick
:
import UIKit
import CometChatSDK
import CometChatUIKitSwift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let uikitSettings = UIKitSettings()
.set(appID: "<#Enter Your App ID Here#>")
.set(authKey: "<#Enter Your AuthKey Here#>")
.set(region: "<#Enter Your Region Code Here#>")
.subscribePresenceForAllUsers()
.build()
CometChatUIKit.init(uiKitSettings: uikitSettings) { result in
switch result {
case .success:
debugPrint("CometChatUIKit initialization succeeded")
let uid = "cometchat-uid-1"
CometChatUIKit.login(uid: uid) { loginResult in
switch loginResult {
case .success:
debugPrint("CometChatUIKit login succeeded")
// open CometChat Conversations
DispatchQueue.main.async {
let conversations = CometChatConversations()
let navController = UINavigationController(rootViewController: conversations)
//Conversations onItemClick click
conversations.set(onItemClick: { [weak self] conversation, indexPath in
// Your code to handle click
})
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
}
case .onError(let error):
debugPrint("CometChatUIKit login failed with error: \(error.description)")
@unknown default:
break
}
}
case .failure(let error):
debugPrint("CometChatUIKit initialization failed with error: \(error.localizedDescription)")
}
}
}
}
Step 6
Rendering the Messages Component
To achieve full messaging functionality, you need to combine the following three components:
Below are the steps to render Messages Screen:

-
Create a new
UIViewController
class named MessagesVC. Inside this class, define two optional variables to store theUser
orGroup
for the chat window.MessagesVC.swiftimport UIKit
import CometChatSDK
import CometChatUIKitSwift
class MessagesVC: UIViewController {
var user: User?
var group: Group?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}These variables will be assigned when a user selects a conversation from CometChatConversations.
-
Define three lazy-loaded UI components for your chat screen
headerView
,messageListView
amdcomposerView
as followsMessagesVC.swift//Setting up header
lazy var headerView: CometChatMessageHeader = {
let headerView = CometChatMessageHeader()
headerView.translatesAutoresizingMaskIntoConstraints = false
headerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
if let user = user { headerView.set(user: user) }
if let group = group { headerView.set(group: group) }
headerView.set(controller: self) //passing controller needs to be mandatory
return headerView
}()
//Setting up composer
lazy var composerView: CometChatMessageComposer = {
let messageComposer = CometChatMessageComposer(frame: .null)
if let user = user { messageComposer.set(user: user) }
if let group = group { messageComposer.set(group: group) }
messageComposer.set(controller: self)
messageComposer.translatesAutoresizingMaskIntoConstraints = false
return messageComposer
}()
//Setting up message list
lazy var messageListView: CometChatMessageList = {
let messageListView = CometChatMessageList(frame: .null)
messageListView.translatesAutoresizingMaskIntoConstraints = false
if let user = user { messageListView.set(user: user) }
if let group = group { messageListView.set(group: group) }
messageListView.set(controller: self)
return messageListView
}() -
Add these components to the view and set up layout constraints in a separate function called
buildUI()
and call it insideviewDidLoad()
.MessagesVC.swiftfunc buildUI() {
view.addSubview(headerView)
view.addSubview(messageListView)
view.addSubview(composerView)
NSLayoutConstraint.activate([
headerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
headerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
headerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
messageListView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
messageListView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
messageListView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
composerView.topAnchor.constraint(equalTo: messageListView.bottomAnchor),
composerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
composerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
composerView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}Now, call buildUI() inside viewDidLoad()
MessagesVCoverride func viewDidLoad() {
super.viewDidLoad()
buildUI()
}The final
MessagesVC.swift
as followsMessagesVC.swiftimport UIKit
import CometChatSDK
import CometChatUIKitSwift
class MessagesVC: UIViewController {
var user: User?
var group: Group?
//Setting up header
lazy var headerView: CometChatMessageHeader = {
let headerView = CometChatMessageHeader()
headerView.translatesAutoresizingMaskIntoConstraints = false
headerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
if let user = user { headerView.set(user: user) }
if let group = group { headerView.set(group: group) }
headerView.set(controller: self) //passing controller needs to be mandatory
return headerView
}()
//Setting up composer
lazy var composerView: CometChatMessageComposer = {
let messageComposer = CometChatMessageComposer(frame: .null)
if let user = user { messageComposer.set(user: user) }
if let group = group { messageComposer.set(group: group) }
messageComposer.set(controller: self)
messageComposer.translatesAutoresizingMaskIntoConstraints = false
return messageComposer
}()
//Setting up message list
lazy var messageListView: CometChatMessageList = {
let messageListView = CometChatMessageList(frame: .null)
messageListView.translatesAutoresizingMaskIntoConstraints = false
if let user = user { messageListView.set(user: user) }
if let group = group { messageListView.set(group: group) }
messageListView.set(controller: self)
return messageListView
}()
func buildUI() {
view.addSubview(headerView)
view.addSubview(messageListView)
view.addSubview(composerView)
NSLayoutConstraint.activate([
headerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
headerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
headerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
messageListView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
messageListView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
messageListView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
composerView.topAnchor.constraint(equalTo: messageListView.bottomAnchor),
composerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
composerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
composerView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: false)
self.view.backgroundColor = .systemBackground
buildUI()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: false)
}
} -
Modify
CometChatConversations
inSceneDelegate.swift
to openMessagesVC.swift
when a user taps on a conversation.SceneDelegate.swiftconversations.set(onItemClick: { [weak self] conversation, indexPath in
let messages = MessagesVC()
messages.group = (conversation.conversationWith as? Group)
messages.user = (conversation.conversationWith as? CometChatSDK.User)
navController.pushViewController(messages, animated: true)
})