> ## 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.

# Block/Unblock User

> Let users block and unblock others in a CometChat iOS UI Kit v6 app: call the SDK, announce the change to the Kit, and swap the composer for an unblock bar.

> [accordion] AI Integration Quick Reference
>
> ```json theme={null}
> {
>   "products": ["CometChatSwiftUI", "CometChatUIKit"],
>   "sdk": ["CometChat.blockUsers(_:onSuccess:onError:)", "CometChat.unblockUsers(_:onSuccess:onError:)"],
>   "announce": "CometChatEvents.emitUserEvent(.userBlocked(user:)) / CometChatUserEvents.ccUserBlocked(user:) — and the unblocked twins",
>   "listen": ".onCometChatUserBlocked / .onCometChatUserUnblocked (SwiftUI), CometChatUserEventListener (UIKit)",
>   "state": "User.blockedByMe, User.hasBlockedMe",
>   "note": "The Kit has no block button or block screen; the action lives on a screen you build (a profile, user details)."
> }
> ```

Blocking stops two people messaging each other. In a v6 app the action lives on a screen you
build — a profile or a user-details page — and the Kit's components follow it once you
announce it on the event bus.

## Overview

* Your screen calls the SDK to block or unblock.
* You set the user's `blockedByMe` and emit `userBlocked` / `userUnblocked`.
* The Kit reacts: **Conversations** drops the chat on a block, **Users** and the
  **message header** redraw the user, and the composer stops sending typing indicators.
* Your chat screen swaps the composer for an "Unblock" bar while you have blocked the user.

## 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 one-to-one chat screen with `CometChatMessageHeader`, `CometChatMessageList` and
  `CometChatMessageComposer`.

## Components

| Piece | Role |
| - | - |
| `CometChat.blockUsers` / `unblockUsers` | The SDK calls that block and unblock. |
| `CometChatEvents` (SwiftUI) / `CometChatUserEvents` (UIKit) | Tell the Kit's components what this user did. |
| `User.blockedByMe`, `User.hasBlockedMe` | Which side has blocked. |
| `CometChatMessageComposer` | Hidden by you while you have blocked the user. |

## Integration Steps

### 1. Bridge the SDK calls

The SDK calls back on its own queue. Bridge it from a `nonisolated` function so the callback
never runs as main-actor code:

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

enum BlockActions {
    nonisolated static func block(uid: String) async throws {
        try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
            CometChat.blockUsers([uid], onSuccess: { _ in continuation.resume() }, onError: { error in
                continuation.resume(throwing: CometChatError(code: error?.errorCode ?? "ERR_UNKNOWN",
                                                             message: error?.errorDescription ?? ""))
            })
        }
    }

    nonisolated static func unblock(uid: String) async throws {
        try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
            CometChat.unblockUsers([uid], onSuccess: { _ in continuation.resume() }, onError: { error in
                continuation.resume(throwing: CometChatError(code: error?.errorCode ?? "ERR_UNKNOWN",
                                                             message: error?.errorDescription ?? ""))
            })
        }
    }
}
```

> [!WARNING]
>
> Do not pass SDK callbacks straight from a view or a view controller. Under Swift 6 a closure
> written in main-actor code is main-actor isolated, and the SDK calling it on its own queue
> stops the app.

### 2. Block from your details screen, and announce it

After the SDK succeeds, mark the user and emit the event. Without the event the conversation
stays in Conversations and the header shows the user as unblocked.

> [tab] SwiftUI
>
> ```swift UserDetailsView.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct UserDetailsView: View {
>     let user: CometChatSDK.User
>     @State private var blocked: Bool
>     @State private var working = false
>
>     init(user: CometChatSDK.User) {
>         self.user = user
>         _blocked = State(initialValue: user.blockedByMe)
>     }
>
>     var body: some View {
>         Button(blocked ? "Unblock" : "Block", role: blocked ? nil : .destructive) {
>             Task { await toggle() }
>         }
>         .disabled(working)
>     }
>
>     private func toggle() async {
>         guard let uid = user.uid else { return }
>         working = true
>         defer { working = false }
>         do {
>             if blocked {
>                 try await BlockActions.unblock(uid: uid)
>                 user.blockedByMe = false
>                 CometChatEvents.emitUserEvent(.userUnblocked(user: user))
>             } else {
>                 try await BlockActions.block(uid: uid)
>                 user.blockedByMe = true
>                 CometChatEvents.emitUserEvent(.userBlocked(user: user))
>             }
>             blocked = user.blockedByMe
>         } catch {
>             print("block failed:", error.localizedDescription)
>         }
>     }
> }
> ```

> [tab] UIKit
>
> ```swift UserDetailsViewController.swift theme={null}
> import UIKit
> import CometChatUIKit
> import CometChatSDK
>
> final class UserDetailsViewController: UIViewController {
>     let user: CometChatSDK.User
>     private let button = UIButton(type: .system)
>
>     init(user: CometChatSDK.User) {
>         self.user = user
>         super.init(nibName: nil, bundle: nil)
>     }
>     required init?(coder: NSCoder) { fatalError() }
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         button.addAction(UIAction { [weak self] _ in self?.toggleBlock() }, for: .primaryActionTriggered)
>         render()
>         // … add the button to the view …
>     }
>
>     private func render() {
>         button.setTitle(user.blockedByMe ? "Unblock" : "Block", for: .normal)
>     }
>
>     private func toggleBlock() {
>         guard let uid = user.uid else { return }
>         button.isEnabled = false
>         Task { [weak self] in
>             guard let self else { return }
>             defer { self.button.isEnabled = true }
>             do {
>                 if self.user.blockedByMe {
>                     try await BlockActions.unblock(uid: uid)
>                     self.user.blockedByMe = false
>                     CometChatUserEvents.ccUserUnblocked(user: self.user)
>                 } else {
>                     try await BlockActions.block(uid: uid)
>                     self.user.blockedByMe = true
>                     CometChatUserEvents.ccUserBlocked(user: self.user)
>                 }
>                 self.render()
>             } catch {
>                 print("block failed:", error.localizedDescription)
>             }
>         }
>     }
> }
> ```

> [!NOTE]
>
> Ask before blocking. The CometChat sample apps confirm with "Block this contact?" and then
> show a toast with the result.

### 3. Swap the composer for an unblock bar

The composer does not hide itself. While you have blocked the user, show your own bar in its
place, and follow the block from anywhere in the app through the event:

> [tab] SwiftUI
>
> ```swift BlockAwareComposer.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> /// The chat's bottom bar: the composer, or your unblock bar while you have blocked the user.
> struct BlockAwareComposer: View {
>     let user: CometChatSDK.User
>     @State private var blockedByMe: Bool
>
>     init(user: CometChatSDK.User) {
>         self.user = user
>         _blockedByMe = State(initialValue: user.blockedByMe)
>     }
>
>     var body: some View {
>         Group {
>             if blockedByMe {
>                 VStack(spacing: 8) {
>                     Text("You have blocked this user")
>                     Button("Unblock User") { Task { await unblock() } }
>                 }
>                 .frame(maxWidth: .infinity)
>                 .padding()
>             } else {
>                 CometChatMessageComposer(user: user)
>             }
>         }
>         .onCometChatUserBlocked { blocked in
>             if blocked.uid == user.uid { blockedByMe = true }
>         }
>         .onCometChatUserUnblocked { unblocked in
>             if unblocked.uid == user.uid { blockedByMe = false }
>         }
>     }
>
>     private func unblock() async {
>         guard let uid = user.uid, (try? await BlockActions.unblock(uid: uid)) != nil else { return }
>         user.blockedByMe = false
>         CometChatEvents.emitUserEvent(.userUnblocked(user: user))
>     }
> }
> ```

> [tab] UIKit
>
> ```swift BlockAwareChatViewController.swift theme={null}
> import UIKit
> import CometChatUIKit
> import CometChatSDK
>
> final class BlockAwareChatViewController: UIViewController, CometChatUserEventListener {
>     let user: CometChatSDK.User
>     let composer = CometChatMessageComposer()
>     let blockedBar = UILabel()
>
>     init(user: CometChatSDK.User) {
>         self.user = user
>         super.init(nibName: nil, bundle: nil)
>     }
>     required init?(coder: NSCoder) { fatalError() }
>
>     override func viewDidLoad() {
>         super.viewDidLoad()
>         composer.set(user: user)
>         blockedBar.text = "You have blocked this user"
>         blockedBar.textAlignment = .center
>         // … lay out the header, the list, the composer and the bar in the same place …
>         apply(user)
>         // Held weakly: the listener goes when the screen does.
>         CometChatUserEvents.addListener("chat-\(user.uid ?? "")", self)
>     }
>
>     func ccUserBlocked(user: CometChatSDK.User) { if user.uid == self.user.uid { apply(user) } }
>     func ccUserUnblocked(user: CometChatSDK.User) { if user.uid == self.user.uid { apply(user) } }
>
>     private func apply(_ user: CometChatSDK.User) {
>         composer.isHidden = user.blockedByMe
>         blockedBar.isHidden = !user.blockedByMe
>     }
> }
> ```

Host the composer, or your bar in its place, over the list as [Conversation List + Message View](https://www.cometchat.com/docs/ui-kit/ios/v6/conversation-message-view.md#step-2-set-up-the-message-screen) shows.

### 4. Hide calls while either side has blocked

The message header hides its call buttons only when you ask. Use both flags — a block either
way should stop calls:

> [tab] SwiftUI
>
> ```swift theme={null}
> let blocked = guideUser.blockedByMe || guideUser.hasBlockedMe
> _ = CometChatMessageHeader(user: guideUser, hideVideoCallButton: blocked, hideVoiceCallButton: blocked)
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let header = CometChatMessageHeader()
> header.set(user: guideUser)
> let blocked = guideUser.blockedByMe || guideUser.hasBlockedMe
> header.hideVideoCallButton = blocked
> header.hideVoiceCallButton = blocked
> ```

## Implementation Flow

1. The user taps **Block** on your details screen and confirms.
2. `CometChat.blockUsers` succeeds; you set `blockedByMe` and emit `userBlocked`.
3. Conversations removes the chat, Users and the header redraw, your chat shows the unblock bar.
4. **Unblock** reverses it with `unblockUsers` and `userUnblocked`; the composer returns.

## Edge Cases

| Case | Behaviour |
| - | - |
| The other person blocked you | `hasBlockedMe` is `true`; you can still see the chat, but messages do not reach them. Hide calls (Step 4). |
| Blocked from another device | The SDK's user is updated on the next fetch; the Kit's bus only carries this device's actions. |
| Presence | The users list and the header ignore presence for a blocked user, either way. |
| A failed SDK call | Nothing changes and no event is emitted — show the error and keep the button. |

## Group vs. User-Level Differences

Blocking is between two users. It does not remove anyone from a group: in a group chat you
keep seeing a blocked user's messages. To keep someone out of a group, ban or kick them — see
[Group Chat](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-group-chat.md).

## Feature Matrix

| Feature | SwiftUI | UIKit |
| - | - | - |
| Block / unblock | `CometChat.blockUsers` / `unblockUsers` | the same |
| Announce | `CometChatEvents.emitUserEvent(.userBlocked(user:))` | `CometChatUserEvents.ccUserBlocked(user:)` |
| Follow the change | `.onCometChatUserBlocked` / `.onCometChatUserUnblocked` | `CometChatUserEventListener` |
| Hide calls | `hideVideoCallButton:`, `hideVoiceCallButton:` | `hideVideoCallButton`, `hideVoiceCallButton` |

## Next Steps

> [card] [Events](https://www.cometchat.com/docs/ui-kit/ios/v6/events.md)
>
> Every event your screens must announce

> [card] [Message Composer](https://www.cometchat.com/docs/ui-kit/ios/v6/message-composer.md)
>
> The composer's options

> [card] [Block Users (SDK)](https://www.cometchat.com/docs/sdk/ios/block-users.md)
>
> The SDK calls in full

> [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.