Enable users to start new 1:1 or group chats in your iOS app using CometChat’s UIKit for iOS by integrating the CreateConversationVC screen.

Overview

The CreateConversation enables users to:
  • Browse CometChat users and groups via native list components.
  • Search within users and groups.
  • Toggle between “Users” and “Groups” tabs using a segmented control.
  • Swipe between lists with a UIPageViewController.
  • Navigate to MessagesVC upon selecting a user or group.

Prerequisites

  • A UIKit-based iOS project.
  • CometChat UIKit for iOS v5 installed via CocoaPods or Swift Package Manager.
  • User authenticated with CometChat.login() before presenting this screen.
  • A UINavigationController embedded in your flow.
  • MessagesVC implemented to handle chat screens.

Components

ComponentRole
UISegmentedControlSwitches between “Users” and “Groups” tabs.
UIPageViewControllerEnables swipe navigation between list views.
CometChatUsersDisplays the list of CometChat users.
CometChatGroupsDisplays the list of CometChat groups.
MessagesVCChat interface, launched upon item selection.
searchControllerProvides search for both users and groups.
CometChatThemeApplies theming (colors, fonts) for UI consistency.
CometChatTypographyDefines text styles for segment labels.

Integration Steps

1. Presenting the Create Conversation Screen

Push CreateConversations to allow starting a chat.
import UIKit
import CometChatSDK

func showCreateConversation() {
    let createVC = CreateConversationVC()
    navigationController?.pushViewController(createVC, animated: true)
}
File reference:
HomeScreenViewController.swift
Provides entry point to the create-conversation flow.

2. Setting Up the User Interface

Build the segmented control and page view controller.
override public func viewDidLoad() {
    super.viewDidLoad()
    buildUI()  // sets up segmentedControl, pageViewController, and searchController
    setupPageViewController()
    segmentedControl.addTarget(self, action: #selector(segmentChanged(_:)), for: .valueChanged)
    navigationItem.searchController = usersViewController.searchController
}
File reference:
CreateConversations.swift
Initializes the UI elements and search integration.

3. Configuring Segmented Control Navigation

Toggle between user and group lists when the segment changes.
@objc public func segmentChanged(_ sender: UISegmentedControl) {
    let index = sender.selectedSegmentIndex
    let direction: UIPageViewController.NavigationDirection = index == 0 ? .reverse : .forward
    navigationItem.searchController = (index == 0)
        ? usersViewController.searchController
        : groupsViewController.searchController
    pageViewController.setViewControllers([pages[index]], direction: direction, animated: true)
}
File reference:
CreateConversations.swift
Keeps the proper search bar and view in sync with the selected tab.

4. Handling Item Selection

Navigate to MessagesVC when a user or group is tapped.
// User tap
public lazy var usersViewController: CometChatUsers = {
    let vc = CometChatUsers()
    vc.set(onItemClick: { [weak self] user, _ in
        let chatVC = MessagesVC()
        chatVC.user = user
        self?.navigationController?.pushViewController(chatVC, animated: true)
    })
    vc.searchController.hidesNavigationBarDuringPresentation = false
    return vc
}()

// Group tap
public lazy var groupsViewController: CometChatGroups = {
    let vc = CometChatGroups()
    vc.set(onItemClick: { [weak self] group, _ in
        let chatVC = MessagesVC()
        chatVC.group = group
        self?.navigationController?.pushViewController(chatVC, animated: true)
    })
    vc.searchController.hidesNavigationBarDuringPresentation = false
    return vc
}()
File reference:
CreateConversations.swift
Routes the user to the appropriate chat screen.

5. Managing Page View Transitions

Implement data source and delegate for smooth swiping.
extension CreateConversationVC: UIPageViewControllerDataSource {
    public func pageViewController(_ pvc: UIPageViewController, viewControllerBefore vc: UIViewController) -> UIViewController? {
        guard let idx = pages.firstIndex(of: vc), idx > 0 else { return nil }
        return pages[idx - 1]
    }
    public func pageViewController(_ pvc: UIPageViewController, viewControllerAfter vc: UIViewController) -> UIViewController? {
        guard let idx = pages.firstIndex(of: vc), idx < pages.count - 1 else { return nil }
        return pages[idx + 1]
    }
}

extension CreateConversationVC: UIPageViewControllerDelegate {
    public func pageViewController(_ pvc: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed, let current = pvc.viewControllers?.first, let idx = pages.firstIndex(of: current) {
            segmentedControl.selectedSegmentIndex = idx
        }
    }
}
File reference:
CreateConversations.swift
Synchronizes the segmented control with page swipes.

Customization Options

  • Segment Styling: Use CometChatTheme to customize tint and font.
  • Labels: Localize or rebrand “USERS” / “GROUPS” labels.
  • Search: Adjust searchController.placeholder and styling.

Filtering & Edge Cases

  • Live Search: Built-in in CometChatUsers and CometChatGroups.
  • Empty States: Components display default “no results” views.
  • Segment Disabled: Hide tabs if only one list is relevant.

Error Handling

  • Load Errors: Use setErrorView() on list components.
  • Navigation Failures: Present an alert if push fails.
  • Blocked Users: Intercept in onItemClick to prevent navigation.

Feature Matrix

FeatureImplementation
Show create screenshowCreateConversation()
Tabbed listsUISegmentedControl + UIPageViewController
Display usersCometChatUsers()
Display groupsCometChatGroups()
Search functionalitysearchController
Navigate to chatMessagesVC(user:) / MessagesVC(group:)

Full Sample App

Explore the complete create-conversation flow: GitHub → SampleApp

UIKit Source Code

Browse the source for CreateConversationVC: GitHub → CreateConversationVC.swift