Provide a detailed view for CometChat groups in your iOS app, enabling users to see group info, join/leave, manage members, and respond to real-time group events.

Overview

GroupDetailsViewController displays comprehensive group information and actions:
  • Group Info: Name, icon, description, and member count.
  • Actions: Join/Leave/Delete group.
  • Member Management: View members, add members, view banned members.
  • Real-Time Updates: Reflects joins, leaves, bans, and ownership changes.

Prerequisites

  • A UIKit-based iOS project.
  • CometChat UIKit for iOS v5 installed via CocoaPods or Swift Package Manager.
  • Valid CometChat App ID, Region, and Auth Key.
  • User logged in with CometChat.login().
  • Navigation stack (UINavigationController) configured.

Components

ComponentRole
CometChatGroupRenders group avatar, name, and metadata.
GroupActionViewCustom view for action buttons (view/add/banned members).
CometChatMessagesViewControllerOpens group chat interface when “Chat” is tapped.
CometChat.joinGroup()Joins public or password-protected groups.
CometChat.leaveGroup()Leaves the current group.
CometChat.deleteGroup()Deletes and exits the group (owners only).
CometChatGroupMembersLists current group members.
CometChatGroupDelegateReceives real-time group events.

Integration Steps

1. Presenting the Group Details Screen

Push GroupDetailsViewController for a selected group.
import UIKit
import CometChatSDK

func showGroupDetails(for group: Group) {
    let detailsVC = GroupDetailsViewController()
    detailsVC.group = group
    navigationController?.pushViewController(detailsVC, animated: true)
}
File reference:
HomeScreenViewController.swift
Initializes and presents the details UI with the correct group context.

2. Setting Up the UI

Configure scroll view, header, and action views.
override public func viewDidLoad() {
    super.viewDidLoad()
    connect()
    view.backgroundColor = CometChatTheme.backgroundColor01
    setupScrollView()
    setupLayout()
    addButtonActions()
}
File reference:
GroupDetailsViewController.swift
Lays out the UI components and registers for group events.

3. Enabling Group Action Buttons

Wire up view/add/banned members actions.
public func addButtonActions() {
    viewMembersView.onActionButtonTapped = { self.viewMembers() }
    addMembersView.onActionButtonTapped = { self.addMembers() }
    bannedMembersView.onActionButtonTapped = { self.banMembers() }
}
File reference:
GroupDetailsViewController.swift
Enables user interaction for member management.

4. Handling Leave and Delete Actions

Provide ownership-aware leave/delete flows.
@objc func showLeaveGroupAlert() { /* ownership transfer or leave logic */ }

@objc func showDeleteGroupAlert() { /* deleteGroup and pop to Home */ }
File reference:
GroupDetailsViewController.swift
Manages group exit, with transfer prompt for owners.

5. Listening for Group Events

Update UI on member joins, leaves, bans, and ownership changes.
func connect() {
    CometChat.addGroupListener(listenerId, self)
    CometChatGroupEvents.addListener(listenerId, self)
}

func disconnect() {
    CometChat.removeGroupListener(listenerId)
    CometChatGroupEvents.removeListener(listenerId)
}

extension GroupDetailsViewController: CometChatGroupDelegate, CometChatGroupEventListener {
    func onGroupMemberJoined(_ action: ActionMessage, user: User, group: Group) { updateGroupInfo(group) }
    func onGroupMemberLeft(_ action: ActionMessage, user: User, group: Group) { /* hide UI if self-left */ }
    // other event methods...
}
File reference:
GroupDetailsViewController.swift
Keeps the group details in sync with back-end events.

Customization Options

  • Header Styling: Use CometChatTheme to customize fonts, colors, and borders.
  • Button Labels: Localize or rebrand action texts.
  • Avatar Placeholder: Provide fallback initials or default images.

Filtering & Edge Cases

  • Private/Protected Groups: Prompt for a password before joining.
  • Already a Member: Hide or disable Join button.
  • Empty Group: Show an empty state when no members.
  • Owner Restrictions: Disable Delete for non-owners.

Error Handling

  • Join Failures: Show alert on network or permission errors.
  • Leave/Delete Errors: Display retry prompt on API failure.
  • Event Errors: Log and notify user if group events fail.

Feature Matrix

FeatureComponent / MethodFile(s)
Show group detailsGroupDetailsViewControllerGroupDetailsViewController.swift
View group membersviewMembersView actionGroupDetailsViewController.swift
Add membersaddMembersView actionGroupDetailsViewController.swift
Ban membersbannedMembersView actionGroupDetailsViewController.swift
Join groupCometChat.joinGroup()GroupDetailsViewController.swift
Leave groupshowLeaveGroupAlert()GroupDetailsViewController.swift
Delete groupshowDeleteGroupAlert()GroupDetailsViewController.swift
Real-time updatesCometChatGroupDelegateGroupDetailsViewController.swift

Full Sample App

Explore the complete Group Details flow: GitHub → SampleApp

UIKit Source Code

Browse the Group Details implementation: GitHub → GroupDetailsViewController.swift