Skip to main content
Since May 1st, 2024, Apple requires all iOS apps to include a privacy manifest. For more details, see Privacy Manifest.
With InstantSearch iOS, you are in control of every widget’s UI. Each widget is represented by a protocol, for which Algolia provides default implementations. If these implementations don’t fit your purpose, you can either customize the existing implementation, or create your own.

Customize the existing implementation

All InstantSearch widgets are built through composition. This enables you to adapt the underlying Controller and add the behavior you want on top of the existing implementation. For example, here’s how you can set the text content type of a UISearchBar embedded in the SearchBarController:
Swift
let searchBarController = SearchBarController(searchBar: UISearchBar())
// if your project targets iOS 13+, we recommend to use TextFieldController(searchBar: UISearchBar())
searchBarController.searchBar.textContentType = .countryName

Implement your own controller

If you want to replace the existing behavior of a default implementation, implement your own Controller.

Example

Here is a custom search box controller that uses UITextView as the input field:
Swift
public class TextFieldController: NSObject, SearchBoxController {
 
  public var onQueryChanged: ((String?) -> Void)?
  public var onQuerySubmitted: ((String?) -> Void)?

  public let textField: UITextField

  public init(textField: UITextField) {
    self.textField = textField
    super.init()
    setupTextField()
  }

  public func setQuery(_ query: String?) {
    textField.text = query
  }

  private func setupTextField() {
    textField.returnKeyType = .search
    textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
    textField.addTarget(self, action: #selector(textFieldDidReturn), for: .editingDidEndOnExit)
  }

  @objc private func textFieldDidChange() {
    onQueryChanged?(textField.text)
  }

  @objc private func textFieldDidReturn() {
    textField.resignFirstResponder()
    onQuerySubmitted?(textField.text)
  }
}
I