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

> Block and unblock users, and retrieve the list of blocked users using the CometChat iOS SDK.

<Accordion title="AI Integration Quick Reference">
  ```swift theme={null}
  // Block users
  CometChat.blockUsers(["UID1", "UID2"],
      onSuccess: { print("Blocked") }, onError: { error in })

  // Unblock users
  CometChat.unblockUsers(["UID1", "UID2"],
      onSuccess: { users in }, onError: { error in })

  // Get blocked users list
  let request = BlockedUserRequest.BlockedUserRequestBuilder.set(limit: 30).build()
  request.fetchNext(onSuccess: { users in }, onError: { error in })
  ```

  **Directions:** `.byMe` | `.me` | `.both` (default)
</Accordion>

Blocking a user prevents all communication between them and the logged-in user — messages, calls, and presence updates are all suppressed. You can block and unblock users by UID, and fetch the blocked users list with filtering and pagination.

## Block Users

Block users to prevent all communication with them. Use `blockUsers()` with an array of UIDs.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let blockUsers = ["UID1", "UID2", "UID3"]

    CometChat.blockUsers(blockUsers, onSuccess: {
                    
       print("Blocked user successfully.")
                    
    }, onError: { (error) in
                    
       print("Blocked user failed with error: \\(error?.errorDescription)")
                    
    })
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    NSMutableArray *users = [NSMutableArray alloc]init];
    [users addObject:@"UID1"];
    [users addObject:@"UID2"];
    [users addObject:@"UID3"];

    [CometChat blockUsers: users onSuccess:^ (NSDictionary *dict) {

      NSLog(@"Blocked user successfully.");
      
    }, onError:^ (CometChatException *error){

      NSLog(@"Blocked user failed with error: %@", error.errorDescription);
    }];
    ```
  </Tab>
</Tabs>

Returns a dictionary with UIDs as keys and `"success"` or `"fail"` as values based on if the block operation for each UID was successful.

## Unblock Users

Unblock previously blocked users using `unblockUsers()` with an array of UIDs.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let unblockUsers = ["UID1", "UID2", "UID3"]

    CometChat.unblockUsers(unblockUsers, onSuccess: { (users) in
                    
       print("Unblocked user successfully.")
                    
    }, onError: { (error) in
                    
       print("Unblocked user failed with error: \\(error?.errorDescription)")
                    
    })
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    NSMutableArray *users = [NSMutableArray alloc]init];
    [users addObject:@"UID1"];
    [users addObject:@"UID2"];
    [users addObject:@"UID3"];

    [CometChat unblockUsers: users onSuccess:^ (NSDictionary *dict){

      NSLog(@"unblocked user successfully.");
      
    }, onError:^ (CometChatException *error){

      NSLog(@"unblocked user failed with error : %@", error.errorDescription);
    }];
    ```
  </Tab>
</Tabs>

Returns a dictionary with UIDs as keys and `"success"` or `"fail"` as values based on if the unblock operation for each UID was successful.

## Get List of Blocked Users

Use `BlockedUsersRequestBuilder` to fetch blocked users with filtering and pagination.

### Set Limit

Sets the number of blocked users to fetch per request.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let blockedUserRequest = BlockedUserRequest.BlockedUserRequestBuilder.set(limit: 20).build();
    ```
  </Tab>
</Tabs>

### Set Search Keyword

Filters blocked users by a search string.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let blockedUserRequest = BlockedUserRequest.BlockedUserRequestBuilder
    .set(searchKeyword: "abc")
    .set(limit: 20)
    .build();
    ```
  </Tab>
</Tabs>

### Set Direction

Filters by block direction:

* `.byMe` — Users blocked by the logged-in user
* `.me` — Users who have blocked the logged-in user
* `.both` — Both directions (default)

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let blockedUserRequest = BlockedUserRequest.BlockedUserRequestBuilder
    .set(searchKeyword: "abc")
    .set(limit: 20)
    .set(direction: .both)
    .build();
    ```
  </Tab>
</Tabs>

After configuring the builder, call `build()` to get the `BlockedUsersRequest` object, then call `fetchNext()` to retrieve blocked users.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let blockedUserRequest = BlockedUserRequest.BlockedUserRequestBuilder(limit: 20).build();

    blockedUserRequest.fetchNext(onSuccess : { (users) in
                
       print("Blocked users: \\(users)")
                
    }, onError : { (error) in
                
       print("Error while fetching the blocked user request:  \\(error?.errorDescription)")

    })
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    BlockedUserRequest *blockedUserRequest = [[[BlockedUserRequestBuilder alloc]initWithLimit:2] build];

    [blockedUserRequest fetchNextOnSuccess: ^(NSArray<User *> * users){

      NSLog(@"Blocked user list fetched successfully.")
      
    } onError:^ (CometChatException *error){

      NSLog(@"Fetching block user list failed with error: %@", error.errorDescription);
      
    }];
    ```
  </Tab>
</Tabs>

The `fetchNext()` method returns an array of [`User`](/sdk/reference/entities#user) objects representing blocked users.

Relevant fields to access on returned users:

| Field        | Type | Description                                      |
| ------------ | ---- | ------------------------------------------------ |
| blockedByMe  | Bool | Whether the logged-in user has blocked this user |
| hasBlockedMe | Bool | Whether this user has blocked the logged-in user |

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Retrieve Users" icon="users" href="/sdk/ios/retrieve-users">
    Fetch and filter user lists
  </Card>

  <Card title="User Presence" icon="circle-dot" href="/sdk/ios/user-presence">
    Track online/offline status of users
  </Card>

  <Card title="User Management" icon="users-gear" href="/sdk/ios/user-management">
    Create, update, and delete users
  </Card>

  <Card title="Flag Message" icon="flag" href="/sdk/ios/flag-message">
    Report inappropriate messages from users
  </Card>
</CardGroup>
