Chat / iOS
Chat iOS v4
Chat iOS
Chat
iOS
Version 4
Home
/
Chat
/
iOS
/
Channel

Retrieve a list of channels

Copy link

You can retrieve a list of OpenChannel objects using the loadNextPage(completionHandler:) method of an OpenChannelListQuery instance and a list of GroupChannel objects using the loadNextPage(completionHandler:) method of a GroupChannelListQuery instance. The GroupChannelListQuery instance returns both public and private group channels that the current user has joined. To retrieve a list of all public group channels regardless of the current user's membership status, use the PublicGroupChannelListQuery instance as described later on this page.

Note: You can also search for specific open channels and group channels with keywords and filters.


Open channels

Copy link

Create an OpenChannelListQuery instance to retrieve a list of open channels matching the specifications set by OpenChannelListQueryParams. After a list of open channels is successfully retrieved, you can access the data of each open channel from the result list through the channels parameter of the callback handler.

class CustomViewController: ViewController {
    var query: OpenChannelListQuery?

    func createQuery() {
        self.query = OpenChannel.createOpenChannelListQuery()
    }

    func loadNextPage() {
        self.query?.loadNextPage { channels, error in
            guard error == nil else {
                // Handle error.
                return
            }

            // A list of open channels is retrieved.
        }
    }
}

Group channels

Copy link

Create a GroupChannelListQuery instance to retrieve a list of group channels matching the specifications set by GroupChannelListQueryParams.

You can set the GroupChannelListQueryParams's includeEmpty property to true to allow users to view empty channels, which include channels that are empty because the view chat history option is turned off to prevent new members from seeing previous conversations. You can determine whether to turn on the chat history option on Sendbird Dashboard under Settings > Chat > Channels > Group channels.

After a list of group channels is successfully retrieved, you can access the data of each channel from the result list through the channels parameter of the callback handler.

class CustomViewController: ViewController {
    var query: GroupChannelListQuery?

    func createQuery() {
        self.query = GroupChannel.createMyGroupChannelListQuery { params in
            // The `params` object is the `GroupChannelListQueryParams` class.
            params.includeEmptyChannel = true
            params.memberStateFilter = .joinedOnly   // Acceptable values are .all, .joinedOnly, .invitedOnly, .invitedByFriend, and .invitedByNonFriend.
            params.order = .latestLastMessage    // Acceptable values are .chronological, .latestLastMessage, .channelNameAlphabetical, and .channelMetaDataValueAlphabetical.
            params.limit = 15
        }
    }

    func loadNextPage() {
        self.query?.loadNextPage { channels, error in
            guard error == nil else {
                // Handle error.
                return
            }

            // A list of group channels is retrieved.
        }
    }
}

All public group channels

Copy link

If you want to retrieve a list of all public group channels regardless of the current user's membership status, use the loadNextPage(completionHandler:) method of a PublicGroupChannelListQuery instance and set membershipFilter to MembershipFilter.ALL in PublicGroupChannelListQueryParams.

After a list of public group channels is successfully retrieved, you can access the data of each channel from the result list through the channels parameter of the callback handler.

class CustomViewController: ViewController {
    var query: PublicGroupChannelListQuery?

    func createQuery() {
        self.query = GroupChannel.createPublicGroupChannelListQuery { params in
            // The params object is created from the PublicGroupChannelListQueryParams class.
            params.includeEmptyChannel = true
            params.publicMembershipFilter = .all  // Acceptable values are .all and .joined.
            params.order = .channelNameAlphabetical  // Acceptable values are .chronological, .channelNameAlphabetical, and .channelMetaDataValueAlphabetical.
            params.limit = 15
        }
    }

    func loadNextPage() {
        self.query?.loadNextPage { channels, error in
            guard error == nil else {
                // Handle error.
                return
            }

            // A list of public group channels is retrieved.
        }
    }
}

Supergroup channels

Copy link

To retrieve a list of Supergroup channels, you can set the superChannelFilter property to .super in either the GroupChannelListQuery or PublicGroupChannelListQuery instance. The superChannelFilter property works much like other search filters. Use GroupChannelListQuery when only searching for group channels that the current user belongs to, and use PublicGroupChannelListQuery when searching for all public group channels regardless of the current user's membership status.

When queried channels are returned, you can check if the channel is a Supergroup channel by looking at whether the isSuper property has a value of true.

Using GroupChannelListQuery

Copy link
class CustomViewController: ViewController {
    var query: GroupChannelListQuery?
    func createQuery() {
        self.query = GroupChannel.createMyGroupChannelListQuery { params in
            // The params object is created from the GroupChannelListQueryParams class.
            params.superChannelFilter = .super
        }
    }
    func loadNextPage() {
        self.query?.loadNextPage { channels, error in
            guard error == nil else {
                // Handle error.
                return
            }
            // A list of Supergroup channels is retrieved.
        }
    }
}

Using PublicGroupChannelListQuery

Copy link
class CustomViewController: ViewController {
    var query: PublicGroupChannelListQuery?
    func createQuery() {
        self.query = GroupChannel.createPublicGroupChannelListQuery { params in
        params.superChannelFilter = .super
        }
    }
    func loadNextPage() {
        self.query?.loadNextPage { channels, error in
            guard error == nil else {
                // Handle error.
                return
            }
        }
    }
}