UIKit iOS v3
UIKit iOS
UIKit
iOS
Home
/
UIKit
/
iOS
This is the new Docs for UIKit v3 beta for iOS. To see the previous Docs, click here.

Search messages

You can search for messages within a group channel using the SBUMessageSearchViewController class, which displays the message search bar and the search results. The message search view is accessible through the Search in channel option in the SBUGroupChannelSettingsViewController class. Once you tap on one of the search results in the message search view, a new group channel view with the selected message as highlighted appears.

Note: If you set the starting point of your chat service to be the channel list or group channel, you can seamlessly guide your users to the message search view.


Initialize

You can start building a message search view through the SBUMessageSearchViewController class. Use the init(channel:) initializer to create the instance and display the view as shown below.

let messageSearchVC = SBUMessageSearchViewController(channel: {CHANNEL})
let navigationController = UINavigationController(rootViewController: messageSearchVC)
present(navigationController, animated: true)

Usage

The following items are key elements of SBUMessageSearchViewController that are used to create a functional message search view.

Module components

In the SBUMessageSearchViewController class, SBUMessageSearchModule and its components are used to create and display the message search view. The module is composed of two components: headerComponent and listComponent.

Property nameTypeDefault value

headerComponent

SBUMessageSearchModule.Header

SBUModuleSet.messageSearchModule.headerComponent

listComponent

SBUMessageSearchModule.List

SBUModuleSet.messageSearchModule.listComponent

When the loadView() method of the view controller is called, the module components get added to the view in the setupView() method of the Sendbird UIKit's view life cycle. Then, the configure method of each module component is called to set the property values and display the view.

headerComponent

The headerComponent in the message search view shows a search bar in the titleView area and a button that searches for the keyword in the user's group channels. Each property corresponds to the elements in the NavigationBar of SBUMessageSearchViewController.

The following table shows the parameters of the configure method of the headerComponent.

Parameter nameType

delegate

SBUMessageSearchModuleHeaderDelegate

theme

SBUMessageSearchTheme

Note: To learn more about the delegate and the properties of the headerComponent, go to the API reference page.

listComponent

The listComponent shows a list of search results that contain the keyword. When a user taps on one of the results, it takes them to the group channel view with the keyword as highlighted. The following table shows the parameters of the configure method of the listComponent.

Parameter nameType

delegate

SBUMessageSearchModuleListDelegate

dataSource

SBUMessageSearchModuleListDataSource

theme

SBUMessageSearchTheme

Note: To learn more about the delegate, data source, and the properties of the listComponent, go to the API reference page.

View model

The SBUMessageSearchViewController class uses a view model that is a type of the SBUMessageSearchViewModel class. The view model is created in the initializer of the view controller through the createViewModel(channel:) method.

The following table shows the parameter of the createViewModel method.

Parameter nameTypeDescription

channel

SBDBaseChannel

Specifies the channel value. (Default: nil)

Note: To learn more about the methods and the event delegates of the view model, go to this API reference page.

SBUMessageSearchViewController properties

To learn more about the properties of SBUMessageSearchViewController, go to the API reference page.


Customization

You can customize the message search view by changing the view controller, module component, and view model that correspond to this key function.

View controller

There are two ways to customize the view controller: change the default view controller value in the global SBUViewControllerSet class or set a single-use custom view controller in the key function.

The custom view controller in the code below is used in the following customization examples.

class CustomViewController: SBUMessageSearchViewController {
    ...
}
  1. Change the value of SBUViewControllerSet.MessageSearchViewController.
SBUViewControllerSet.MessageSearchViewController = CustomViewController.self

// The view controller will now use `CustomViewController` as the default value.
  1. Use a one-time custom view controller in the message search view.
let messageSearchVC = CustomViewController(channel: {CHANNEL})
let navigationController = UINavigationController(rootViewController: messageSearchVC)
present(navigationController, animated: true)

Module component

There are two ways to customize a module component: change the default module component value in the global SBUModuleSet.messageSearchModule class or set a single-use custom module component in the view controller.

The custom header component in the code below is used in the following customization examples.

class CustomHeader: SBUModuleSet.messageSearchModule.Header {
    ...
}
  1. Change the value of SBUModuleSet.messageSearchModule.headerComponent.
SBUModuleSet.messageSearchModule.headerComponent = CustomHeader()

let messageSearchVC = SBUViewControllerSet.MessageSearchViewController(channel: {CHANNEL}})

// The `headerComponent` in `SBUMessageSearchViewController` will now use `customHeader` as the default value.
  1. Change the module component in SBUMessageSearchViewController.
let messageSearchVC = SBUViewControllerSet.MessageSearchViewController(channel: {CHANNEL})
messageSearchVC.headerComponent = CustomHeader()

Note: To learn more about the methods of SBUMessageSearchModule, go to the API reference page.

View model

In order to use a customized view model or customize the existing view model's event delegate, you must override the view controller.

  1. Use a customized view model.
class CustomViewController: SBUMessageSearchViewController {
    override func createViewModel(...) {
        // Set up viewModel to customized view model object.
        self.viewModel = CustomViewModel()
        ...
    }
}
  1. Customize the view model's event delegate.
extension CustomViewController: SBUMessageSearchViewModelDelegate {
    override func searchViewModel(
      _ viewModel: SBUMessageSearchViewModel,
      didChangeSearchResults results: [SBDBaseMessage],
      needsToReload: Bool
    ) {
        // Implement your code here.
    }
}

MessageSearchQueryBuilder

In SBUMessageSearchViewController, you can customize the query data used to search for keywords in the channel by using MessageSearchQueryBuilder. There are two ways to customize the query builder: use a customized query builder in the view controller or override the createViewModel() method.

  1. Use a customized query builder.
let messageSearchVC = SBUMessageSearchViewController(channel: {CHANNEL})
messageSearchVC.customMessageSearchQueryBuilder = {builder in
    builder.exactMatch = true
    builder.limit = 10
    ...
}
  1. Override createViewModel().
class CustomViewController: SBUMessageSearchViewController {
    override func createViewModel(...) {
        // Set `viewModel` to the `CustomViewModel` object.
        self.viewModel = SBUMessageSearchViewModel(
            channel: channel,
            messageSearchQueryBuilder: { builder in
                builder.exactMatch = true
                builder.limit = 10
                ...
            },
            delegate: self
        )
    }
}