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 manual user searches.

Prerequisites

  • UIKit-based iOS project.
  • CometChat UIKit for iOS v5 installed via CocoaPods or Swift Package Manager.
  • Valid CometChat App ID, Region, and Auth Key.
  • Functional group chat via CometChatMessageList.
  • A one-on-one chat screen (CometChatMessages or custom) and navigation flow configured.

Components

ComponentRole
CometChatMessageListDisplays group messages; 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.
CometChatUIKit.getUser(uid:)Retrieves the User object for the selected message sender.
CometChatUIKit.getConversationWith(user:)Creates or fetches the 1-on-1 conversation instance.
UIViewController (Navigation)Pushes or presents the private chat screen (CometChatMessages).

Integration Steps

1. Control Option Visibility via ViewModel

Dynamically show or hide Message Privately based on app context.
public var hideMessagePrivatelyOption: Bool = false {
    didSet {
        viewModel.hideMessagePrivatelyOption = hideMessagePrivatelyOption
    }
}
File reference:
CometChatMessageList.swift
Ensures the option only appears when appropriate (e.g., user permissions).

2. Handle Private Chat Navigation

Retrieve the sender and initiate a private 1-on-1 chat.
if let uid = selectedMessage.sender?.uid {
    CometChatUIKit.getUser(uid: uid) { user in
        CometChatUIKit.getConversationWith(user: user) { conversation in
            let messagesVC = CometChatMessages(conversation: conversation)
            navigationController?.pushViewController(messagesVC, animated: true)
        }
    }
}
File reference:
CometChatMessageList.swift
Automates transition from group context to private conversation.

Implementation Flow

  1. Long-press a group message in CometChatMessageList.
  2. Options menu appears with Message Privately.
  3. User taps Message Privately.
  4. App fetches User via CometChatUIKit.getUser(uid:).
  5. Retrieves or creates conversation via CometChatUIKit.getConversationWith(user:).
  6. Pushes CometChatMessages onto the navigation stack.

Customization Options

  • Styling: Override CometChatMessageOption UI (icons, fonts, colors).
  • Availability: Control via viewModel.hideMessagePrivatelyOption.
  • Extend Options: Add additional actions in MessageDataSource.getMessageOptions(for:).

Filtering & Edge Cases

  • Blocked Users: Hide option if the sender is in block list.
  • Existing Conversations: Reuse existing thread via getConversationWith.
  • Unavailable Users: Skip option or show disabled state if user data missing.

Error Handling & Blocked-User Handling

  • Block State: Catch errors from getUser/getConversationWith and alert user.
  • Network Failures: Present retry or toast on navigation errors.
  • Invalid Data: Disable option if sender.uid is nil.

Optional Context-Specific Notes

  • Only available in group chat screens (CometChatMessageList).
  • Hidden automatically in direct/private chat views.

Summary / Feature Matrix

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

Full Sample App

Explore this feature in the CometChat SampleApp: GitHub → SampleApp

UIKit Source Code

Browse the message list component: GitHub → CometChatMessageList.swift