> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Group Management

> Create and join groups, list and manage members, add members, transfer ownership, and leave or delete groups in a CometChat iOS UI Kit v6 app.

> [accordion] AI Integration Quick Reference
>
> ```json theme={null}
> {
>   "products": ["CometChatSwiftUI", "CometChatUIKit"],
>   "components": ["CometChatGroups", "CometChatGroupMembers", "CometChatUsers", "CometChatMessageHeader", "CometChatMessageList", "CometChatMessageComposer"],
>   "built in": "joining a public group on tap; kick, ban and scope change in CometChatGroupMembers",
>   "your code": "create, add members, transfer ownership, leave, delete — SDK calls, then a CometChatEvents / CometChatGroupEvents emit",
>   "join a password group": "GroupsStore.join(_:password:) — store (SwiftUI) / groupsStore (UIKit)",
>   "events": ["groupCreated", "memberJoined", "membersAdded", "ownershipChanged", "groupLeft", "groupDeleted"]
> }
> ```

A group chat is more than a conversation: people create groups, join them, and the group's
admins manage who is in it. The Kit lists groups and members and handles kick, ban and scope
changes itself. Creating a group, adding members, transferring ownership, leaving and
deleting are SDK calls on screens you build — after each, you announce it so the Kit's lists
update.

## Overview

| Task | Who does it |
| - | - |
| List and search groups | `CometChatGroups` |
| Join a public group | `CometChatGroups`, on tap |
| Join a password group | you, with `join(_:password:)` on the list's store |
| List members; kick, ban, change scope | `CometChatGroupMembers` |
| Create a group, add members, transfer ownership, leave, delete | you: an SDK call, then an event |

## Prerequisites

* The Kit is set up and a user is logged in — see [Getting Started](https://www.cometchat.com/docs/ui-kit/ios/v6/getting-started-swiftui.md).
* A chat screen that takes a `Group`.

## Integration Steps

### 1. Bridge the SDK calls

The SDK calls back on its own queue, so bridge each call from a `nonisolated` function.
`SDKBox` (from `CometChatCore`) carries an SDK object back to the main actor:

```swift GroupActions.swift theme={null}
import CometChatSDK
import CometChatCore

enum GroupActions {
    nonisolated static func create(guid: String, name: String) async throws -> SDKBox<CometChatSDK.Group> {
        try await withCheckedThrowingContinuation { continuation in
            let group = CometChatSDK.Group(guid: guid, name: name, groupType: .public, password: nil)
            CometChat.createGroup(group: group, onSuccess: { continuation.resume(returning: SDKBox($0)) },
                                  onError: { continuation.resume(throwing: failure($0)) })
        }
    }

    nonisolated static func add(uids: [String], to guid: String) async throws {
        try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
            let members = uids.map { CometChatSDK.GroupMember(UID: $0, groupMemberScope: .participant) }
            CometChat.addMembersToGroup(guid: guid, groupMembers: members, onSuccess: { _ in continuation.resume() },
                                        onError: { continuation.resume(throwing: failure($0)) })
        }
    }

    nonisolated static func transferOwnership(to uid: String, of guid: String) async throws {
        try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
            CometChat.transferGroupOwnership(UID: uid, GUID: guid, onSuccess: { _ in continuation.resume() },
                                             onError: { continuation.resume(throwing: failure($0)) })
        }
    }

    nonisolated static func delete(guid: String) async throws {
        try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
            CometChat.deleteGroup(GUID: guid, onSuccess: { _ in continuation.resume() },
                                  onError: { continuation.resume(throwing: failure($0)) })
        }
    }

    nonisolated private static func failure(_ e: CometChatException?) -> CometChatError {
        CometChatError(code: e?.errorCode ?? "ERR_UNKNOWN", message: e?.errorDescription ?? "")
    }
}
```

### 2. Create a group

Create it, then emit `groupCreated` so Conversations and Groups show it without a refetch:

> [tab] SwiftUI
>
> ```swift theme={null}
> let created = try await GroupActions.create(guid: "design-\(Int(Date().timeIntervalSince1970))", name: "Design").value
> CometChatEvents.emitGroupEvent(.groupCreated(group: created))
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let created = try await GroupActions.create(guid: "design-\(Int(Date().timeIntervalSince1970))", name: "Design").value
> CometChatGroupEvents.ccGroupCreated(group: created)
> ```

### 3. Join a group from the list

A tap on a **public** group you have not joined joins it under a "Joining Group" alert, emits
`memberJoined`, and then calls `onItemClick` with the joined group. A **password** group you
have not joined comes to `onItemClick` unjoined: check `hasJoined`, ask for the password and
join through the list's store.

> [tab] SwiftUI
>
> ```swift GroupsView.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct GroupsView: View {
>     @State private var store = GroupsStore()
>     @State private var opened: CometChatSDK.Group?
>
>     var body: some View {
>         CometChatGroups(store: store, onItemClick: { group in
>             if group.groupType == .password && !group.hasJoined {
>                 Task {
>                     // Ask the user for the password; "secret" stands in for it here.
>                     if let joined = await store.join(group, password: "secret") { opened = joined }
>                 }
>             } else {
>                 opened = group
>             }
>         })
>     }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let groups = CometChatGroups()
> groups.set(onItemClick: { [weak groups] group, _ in
>     guard let groups else { return }
>     if group.groupType == .password && !group.hasJoined {
>         Task {
>             // Ask the user for the password; "secret" stands in for it here.
>             if let joined = await groups.groupsStore.join(group, password: "secret") {
>                 print("open", joined.guid)
>             }
>         }
>     } else {
>         print("open", group.guid)
>     }
> })
> ```

A failed join calls `onError` and nothing else. A successful one emits `memberJoined` for you.

### 4. View and manage members

`CometChatGroupMembers` lists the group's members with their scope. Its long-press menu and
swipe actions offer **Kick**, **Ban** and **Change Scope** to the people allowed to use them:
the owner over anyone, an admin over moderators and participants, a moderator may kick
participants. The Kit emits the matching events itself.

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatGroupMembers(group: guideGroup)
>
> // Turn individual actions off:
> CometChatGroupMembers(group: guideGroup, disableKick: true, disableBan: true)
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let members = CometChatGroupMembers(group: guideGroup)
> members.hideBanMemberOption = true
> navigationController?.pushViewController(members, animated: true)
> ```

### 5. Add members

Let the user pick people from `CometChatUsers` in multiple selection, add them, and emit
`membersAdded` with one action message per member — the message list shows an "added" line
for each:

> [tab] SwiftUI
>
> ```swift AddMembersView.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct AddMembersView: View {
>     let group: CometChatSDK.Group
>
>     var body: some View {
>         // onSelection fires from the list's tick with everyone picked.
>         CometChatUsers(selectionMode: .multiple, onSelection: { users in
>             Task { await add(users) }
>         })
>     }
>
>     private func add(_ users: [CometChatSDK.User]) async {
>         guard let me = CometChatSession.loggedInUser,
>               (try? await GroupActions.add(uids: users.compactMap(\.uid), to: group.guid)) != nil else { return }
>         group.membersCount += users.count
>         let actions = users.map { user -> ActionMessage in
>             let action = ActionMessage()
>             action.action = .added
>             action.receiver = group
>             action.receiverUid = group.guid
>             action.receiverType = .group
>             action.messageCategory = .action
>             action.sender = me
>             action.actionBy = me
>             action.actionOn = user
>             action.actionFor = group
>             action.sentAt = Int(Date().timeIntervalSince1970)
>             return action
>         }
>         CometChatEvents.emitGroupEvent(.membersAdded(actions: actions, users: users, group: group, addedBy: me))
>     }
> }
> ```

> [tab] UIKit
>
> ```swift AddMembersViewController.swift theme={null}
> import UIKit
> import CometChatUIKit
> import CometChatSDK
>
> final class AddMembersViewController: CometChatUsers {
>     var group: CometChatSDK.Group?
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         selectionMode = .multiple
>         // Called from the tick with everyone picked.
>         set(onSelection: { [weak self] users in
>             Task { await self?.add(users) }
>         })
>     }
>
>     private func add(_ users: [CometChatSDK.User]) async {
>         guard let group, let me = CometChatSession.loggedInUser,
>               (try? await GroupActions.add(uids: users.compactMap(\.uid), to: group.guid)) != nil else { return }
>         group.membersCount += users.count
>         let actions = users.map { user -> ActionMessage in
>             let action = ActionMessage()
>             action.action = .added
>             action.receiver = group
>             action.receiverUid = group.guid
>             action.receiverType = .group
>             action.messageCategory = .action
>             action.sender = me
>             action.actionBy = me
>             action.actionOn = user
>             action.actionFor = group
>             action.sentAt = Int(Date().timeIntervalSince1970)
>             return action
>         }
>         CometChatGroupEvents.ccGroupMemberAdded(messages: actions, usersAdded: users, groupAddedIn: group, addedBy: me)
>         navigationController?.popViewController(animated: true)
>     }
> }
> ```

### 6. Transfer ownership

The owner must hand the group to someone before leaving it. Pick a member in single
selection, transfer, and emit `ownershipChanged`:

> [tab] SwiftUI
>
> ```swift TransferOwnershipView.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct TransferOwnershipView: View {
>     let group: CometChatSDK.Group
>     @Environment(\.dismiss) private var dismiss
>
>     var body: some View {
>         CometChatGroupMembers(group: group, excludeOwner: true, selectionMode: .single,
>                               onItemClick: { member in Task { await transfer(to: member) } })
>     }
>
>     private func transfer(to member: CometChatSDK.GroupMember) async {
>         guard let uid = member.uid,
>               (try? await GroupActions.transferOwnership(to: uid, of: group.guid)) != nil else { return }
>         group.owner = uid
>         CometChatEvents.emitGroupEvent(.ownershipChanged(group: group, newOwner: member))
>         dismiss()
>     }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let members = CometChatGroupMembers(group: guideGroup)
> members.set(onItemClick: { member, _ in
>     guard let uid = member.uid else { return }
>     Task {
>         guard (try? await GroupActions.transferOwnership(to: uid, of: guideGroup.guid)) != nil else { return }
>         guideGroup.owner = uid
>         CometChatGroupEvents.ccOwnershipChanged(group: guideGroup, newOwner: member)
>     }
> })
> ```

### 7. Delete a group

Delete it, then emit `groupDeleted` so it leaves Conversations and Groups:

> [tab] SwiftUI
>
> ```swift theme={null}
> try await GroupActions.delete(guid: guideGroup.guid)
> CometChatEvents.emitGroupEvent(.groupDeleted(group: guideGroup))
> ```

> [tab] UIKit
>
> ```swift theme={null}
> try await GroupActions.delete(guid: guideGroup.guid)
> CometChatGroupEvents.ccGroupDeleted(group: guideGroup)
> ```

Leaving is the same shape: `CometChat.leaveGroup(GUID:onSuccess:onError:)`, then
`groupLeft(action:user:group:)` / `ccGroupLeft(action:leftUser:leftGroup:)` with an action
message built like the ones in step 5 (`action = .left`, `actionOn` yourself).

## Implementation Flow

1. The user creates or finds a group; a public group is joined on tap.
2. The chat opens with `CometChatMessageHeader`, `CometChatMessageList` and
   `CometChatMessageComposer` given the group.
3. Your group-details screen offers members, add members, transfer ownership, leave and delete.
4. Each of your actions emits its event; every list on screen follows.

## Error Handling & Edge Cases

| Case | Behaviour |
| - | - |
| A failed SDK call | Emit nothing; show the error. |
| A banned user | Cannot join; the join fails with an error. |
| The owner leaves | The server refuses until ownership is transferred (step 6). |
| Your scope | The members list reads it from your own row; until that row is loaded you are treated as a participant. |
| A forgotten event | The lists go stale until they refetch — see [Events](https://www.cometchat.com/docs/ui-kit/ios/v6/events.md). |

## Feature Matrix

| Feature | SwiftUI | UIKit |
| - | - | - |
| Groups list | `CometChatGroups(onItemClick:)` | `CometChatGroups()`, `set(onItemClick:)` |
| Join a password group | `store.join(_:password:)` | `groupsStore.join(_:password:)` |
| Members | `CometChatGroupMembers(group:)` | `CometChatGroupMembers(group:)` |
| Turn off kick / ban / scope | `disableKick:`, `disableBan:`, `disableChangeScope:` | `hideKickMemberOption`, `hideBanMemberOption`, `hideScopeChangeOption` |
| Create | SDK + `.groupCreated` | SDK + `ccGroupCreated` |
| Add members | SDK + `.membersAdded` | SDK + `ccGroupMemberAdded` |
| Transfer ownership | SDK + `.ownershipChanged` | SDK + `ccOwnershipChanged` |
| Leave / delete | SDK + `.groupLeft` / `.groupDeleted` | SDK + `ccGroupLeft` / `ccGroupDeleted` |

## Next Steps

> [card] [Groups](https://www.cometchat.com/docs/ui-kit/ios/v6/groups.md)
>
> The groups list in full

> [card] [Group Members](https://www.cometchat.com/docs/ui-kit/ios/v6/group-members.md)
>
> Members, scopes and actions

> [card] [Groups (SDK)](https://www.cometchat.com/docs/sdk/ios/groups-overview.md)
>
> Every group call in the SDK

> [card] [All Guides](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-overview.md)
>
> Browse the other guides


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.