Skip to main content
Enable users to start a private one-on-one chat with a group member directly from a group chat screen using CometChat’s UIKit for iOS.

Overview

The Message Privately feature allows users to long-press a message in a group chat and select Message Privately to transition into a private conversation with the original sender. This streamlines side discussions and follow-ups without requiring manual user searches.

Prerequisites

Before implementing this feature, ensure you have:
  1. Completed Getting Started setup
  2. CometChat UIKit v5+ installed
  3. User logged in with CometChatUIKit.login()

Components

ComponentDescription
CometChatMessageListDisplays group messages and handles long-press to show options
CometChatMessageOptionDefines the Message Privately option in the context menu
MessageDataSourceSupplies the messagePrivatelyOption in the options array
CometChatMessageListViewModelManages UI state, including hideMessagePrivatelyOption
CometChatMessagesEntry point for rendering or pushing the private chat interface
CometChat.getUser(UID:onSuccess:onError:)Retrieves the User object for the selected message sender
CometChatUIEvents.openChat(user:group:)Opens the chat interface for a user or group
UIViewController (Navigation)Pushes or presents the private chat screen (CometChatMessages)

Integration Steps

Step 1: Control Option Visibility via ViewModel

Dynamically show or hide Message Privately based on app context:
import CometChatUIKitSwift

// Control visibility of the Message Privately option
public var hideMessagePrivatelyOption: Bool = false {
    didSet {
        // Sync with view model
        viewModel.hideMessagePrivatelyOption = hideMessagePrivatelyOption
    }
}
This ensures the option only appears when appropriate (e.g., based on user permissions). File reference: CometChatMessageList.swift

Step 2: Handle Private Chat Navigation

Retrieve the sender and initiate a private 1-on-1 chat:
import UIKit
import CometChatSDK
import CometChatUIKitSwift

func startPrivateChat(with selectedMessage: BaseMessage) {
    // Get the sender from the selected message
    if let user = selectedMessage.sender {
        // Open the private chat using CometChatUIEvents
        DispatchQueue.main.async {
            CometChatUIEvents.openChat(user: user, group: nil)
        }
    }
}

// Alternative: Manual navigation with user fetch
func startPrivateChatManual(with selectedMessage: BaseMessage) {
    guard let uid = selectedMessage.sender?.uid else { return }
    
    // Fetch the user object
    CometChat.getUser(UID: uid) { [weak self] user in
        guard let user = user else { return }
        
        DispatchQueue.main.async {
            // Navigate to the private chat screen
            let messagesVC = CometChatMessages()
            messagesVC.set(user: user)
            self?.navigationController?.pushViewController(messagesVC, animated: true)
        }
    } onError: { error in
        print("Error fetching user: \(error?.errorDescription ?? "")")
    }
}
This automates the transition from group context to private conversation. File reference: CometChatMessageList.swift

Implementation Flow

StepAction
1Long-press a group message in CometChatMessageList
2Options menu appears with Message Privately
3User taps Message Privately
4App gets User from message.sender
5Opens chat via CometChatUIEvents.openChat(user:group:)
6Pushes CometChatMessages onto the navigation stack

Customization Options

Styling

Override CometChatMessageOption UI elements:
// Customize the message option appearance
let privateOption = CometChatMessageOption(
    id: "message-privately",
    title: "Message Privately",
    icon: UIImage(systemName: "person.fill"),
    backgroundColor: .systemBlue
)

Availability

Control visibility via the view model:
// Hide the option for specific scenarios
viewModel.hideMessagePrivatelyOption = true

Extend Options

Add additional actions in MessageDataSource.getMessageOptions(for:):
func getMessageOptions(for message: BaseMessage) -> [CometChatMessageOption] {
    var options = [CometChatMessageOption]()
    
    // Add message privately option
    if !hideMessagePrivatelyOption {
        options.append(messagePrivatelyOption)
    }
    
    // Add custom options
    options.append(customOption)
    
    return options
}

Edge Cases

ScenarioHandling
Blocked usersHide option if the sender is in block list
Existing conversationsReuse existing thread via getConversationWith
Unavailable usersSkip option or show disabled state if user data is missing

Error Handling

Error TypeSolution
Block stateCatch errors from CometChat.getUser and alert user
Network failuresPresent retry or toast on navigation errors
Invalid dataDisable option if sender.uid is nil

Additional Notes

  • This feature is only available in group chat screens (CometChatMessageList)
  • The option is hidden automatically in direct/private chat views

Feature Matrix

FeatureComponent / MethodFile(s)
Show options menugetMessageOptions(for:)MessageDataSource.swift
Toggle Message PrivatelyviewModel.hideMessagePrivatelyOptionCometChatMessageList.swift, MessageListViewModel.swift

Sample App

Explore this feature in the CometChat SampleApp

UIKit Source

Browse the message list component

Users

Display and manage users

Conversations

View and manage conversations

Group Members

View group membership