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

# Retrieve Users

> Fetch, filter, search, and sort users using the CometChat iOS SDK. Includes pagination, role-based filtering, tag support, and online status filtering.

<Accordion title="AI Integration Quick Reference">
  ```swift theme={null}
  // Fetch users list
  let request = UsersRequest.UsersRequestBuilder()
      .set(limit: 30).build()
  request.fetchNext(onSuccess: { users in }, onError: { error in })

  // Get specific user details
  CometChat.getUser(UID: "UID", onSuccess: { user in }, onError: { error in })

  // Get logged-in user
  let me = CometChat.getLoggedInUser()
  ```
</Accordion>

The CometChat SDK provides methods to retrieve the logged-in user, fetch filtered user lists, and look up individual users by UID. All user methods return [`User`](/sdk/reference/entities#user) objects.

## Get the Logged-In User

Use `getLoggedInUser()` to get the current user's details. Returns `nil` if no user is logged in.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let user = CometChat.getLoggedInUser();
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    User *user = [CometChat getLoggedInUser];
    ```
  </Tab>
</Tabs>

This method returns a [`User`](/sdk/reference/entities#user) object containing all the information related to the logged-in user.

## Retrieve List of Users

Use `UsersRequestBuilder` to fetch users with filtering, searching, and pagination.

### Set Limit

Sets the number of users to fetch per request.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: 30)
    .build()
    ```
  </Tab>
</Tabs>

### Set Search Keyword

Filters users by a search string.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: 30)
    .set(searchKeyword: "abc")
    .build()
    ```
  </Tab>
</Tabs>

### Search In

Specifies which user properties to search. Works with `set(searchKeyword:)`. By default, searches both UID and name.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let searchKeyword = "super"
    let searchIn = ["uid", "name"]
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: 30)
    .set(searchKeyword: searchKeyword)
    .searchIn(searchIn)
    .build()
    ```
  </Tab>
</Tabs>

### Set Status

Filters users by online status:

* `CometChat.UserStatus.online` — Only online users
* `CometChat.UserStatus.offline` — Only offline users

If not set, returns all users.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: 30)
    .set(status: .online)
    .build()
    ```
  </Tab>
</Tabs>

### Hide Blocked Users

When `true`, excludes users blocked by the logged-in user from the results.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: 30)
    .hideBlockedUsers(true)
    .build()
    ```
  </Tab>
</Tabs>

### Set Roles

Filters users by specified roles.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: 30)
    .set(roles : ["role1","role2"])
    .build()
    ```
  </Tab>
</Tabs>

### Friends Only

When `true`, returns only friends of the logged-in user.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: 30)
    .friendsOnly(true)
    .build();
    ```
  </Tab>
</Tabs>

### Set Tags

Filters users by specified tags.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: 30)
    .set(tags: ["tag1", "tag2"])
    .build();
    ```
  </Tab>
</Tabs>

### With Tags

When `true`, includes tag data in the returned user objects.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: 30)
    .withTags(true)
    .build()
    ```
  </Tab>
</Tabs>

### Set UIDs

Fetches specific users by their UIDs. Maximum 25 users per request.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let UIDs = ["cometchat-uid-1", "cometchat-uid-2"]
    let limit = 30
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: limit)
    .setUIDs(UIDs)
    .build();
    ```
  </Tab>
</Tabs>

### Sort By

Sorts the user list by a specific property. Default sort order: `status → name → UID`. Pass `"name"` to sort by `name → UID`.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let sortBy = "name"
    let limit = 30
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: limit)
    .sortBy(sortBy)
    .build();
    ```
  </Tab>
</Tabs>

### Sort By Order

Sets the sort order. Default is ascending. Use `.desc` for descending.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let limit = 30
    let usersRequest = UsersRequest.UsersRequestBuilder()
    .set(limit: limit)
    .sortOrder(.desc)
    .build();
    ```
  </Tab>
</Tabs>

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

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let limit = 20;

    let usersRequest = UsersRequest.UsersRequestBuilder(limit: limit).build();

    usersRequest.fetchNext(onSuccess: { (users) in

      for user in users {

         print("User: " + user.stringValue())
      }

    }) { (error) in

      print("User list fetching failed with error: " + error!.errorDescription);
    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    NSInteger limit = 30 ;

    UsersRequest *userRequest = [[[UsersRequestBuilder alloc]initWithLimit:limit] build];

    [userRequest fetchNextOnSuccess:^(NSArray<User *> * users) {
        
        for (User *user in users) {

          NSLog(@"User: %@ ",[user stringValue]);
        }
        
    } onError:^(CometChatException * error) {
        
        NSLog(@"User list fetching failed with error: %@",[error errorDescription]);
        
    }];
    ```
  </Tab>
</Tabs>

## Retrieve Particular User Details

Use `getUser()` to fetch a specific user's details by UID.

<Tabs>
  <Tab title="Swift">
    ```swift theme={null}
    let uid  = "cometchat-uid-1";

    CometChat.getUser(UID: uid, onSuccess: { (user) in

      print("User: " + user.stringValue())

    }) { (error) in

      print("User fetching failed with error: " + error.errorDescription);
    }
    ```
  </Tab>

  <Tab title="Objective C">
    ```objc theme={null}
    NSString *UID = @"cometchat-uid-1";

    [CometChat getUserWithUID:UID onSuccess:^(User * user) {
        
        NSLog(@"User: %@",[user stringValue]);
        
    } onError:^(CometChatException * error) {
        
        NSLog(@"User fetching failed with error: %@",[error errorDescription]);
    }];
    ```
  </Tab>
</Tabs>

| Parameter | Description                                                   |
| --------- | ------------------------------------------------------------- |
| UID       | The `UID` of the user for whom the details are to be fetched. |

It returns a [`User`](/sdk/reference/entities#user) object containing the details of the user.

***

## Next Steps

<CardGroup cols={2}>
  <Card title="User Presence" icon="circle-dot" href="/sdk/ios/user-presence">
    Track and subscribe to user online/offline status
  </Card>

  <Card title="Block Users" icon="ban" href="/sdk/ios/block-users">
    Block and unblock users from your application
  </Card>

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

  <Card title="Retrieve Conversations" icon="comments" href="/sdk/ios/retrieve-conversations">
    Fetch conversation lists for your chat UI
  </Card>
</CardGroup>
