- A list to display search results
- A search box to type your query
- Statistics about the current search
- A facet list for filtering results
Installation
To use InstantSearch iOS, you need an Algolia account. You can create a new account, or use the following credentials:- APP ID:
latency - Search API Key:
1f6fd3a6fb973cb08419fe7d288fa4db - Index name:
bestbuy
Create a new project
In Xcode, create a new project:- On the Template screen, select Single View Application and click next.
- Specify your Product Name, select Swift as the language and iPhone for **Devices##, and then create.

Prepare your storyboard
After generation, the project directly shows the main view controller. Embed this into the Navigation Controller.- Open
Main.storyboard - Select
ViewControllerin the View Controller scene - Editor > Embed in > Navigation Controller

Add InstantSearch dependency
To add the InstantSearch package dependency to your Xcode project, you need a dependency manager. This can be the Swift Package Manager, or CocoaPods.Swift package manager
- Select File > Swift Packages > Add Package Dependency and enter this repository URL:
https://github.com/algolia/instantsearch-ios - You can also go to your target’s General pane, and in the Frameworks, Libraries, and Embedded Content section, click the
+button, select Add Other, and choose Add Package Dependency. - In the package products selection dialog, select both the InstantSearch and InstantSearchCore dependencies.
CocoaPods
- If you don’t have CocoaPods installed on your machine, open your terminal and run
sudo gem install cocoapods. - In your terminal, got to the root directory of your project and run the
pod initcommand. This command generates a Podfile for you. - Open your Podfile and add
pod 'InstantSearch', '~> 7'below your target. - In your terminal, run
pod update. - Close your Xcode project. In your terminal, at the root of your project, run the
open projectName.xcworkspacecommand (replacingprojectNamewith the actual name of your project).
Implementation
Xcode automatically generates aViewController.swift file when you create a Single View Application. Open this file, and add the following import statement at the top.
Swift
Define your record structure
Define a structure that represent a record in your index. For simplicity’s sake, this structure only provides the name of the product. The structure must conform to theCodable protocol to work properly with InstantSearch. Add the following structure definition to the ViewController.swift file:
Swift
Declare hits view controller
In this tutorial search results view controller is represented by aUITableViewController which conforms to HitsController protocol. The following example binds the name of the fetched item to the cell’s textLabel s text property.
Swift
HitsController documentation for more on integrating and customizing the `Hits’ widget.
Complete the view controller
To complete the main view controller of your application, declare aUISearchController.
This UIKit component manages the display of search results based on interactions with a search box.
It provides a search box and only requires a search results controller as a parameter.
Add a hitsViewController field to the view controller, with the type declared in the previous step.
Pass it as the searchResultsController when creating the search controller.
Swift
Initialize your searcher
With the necessary view controllers in place, it’s time to add the search logic. The central part of the search experience is theSearcher,
which performs search requests and retrieves results.
Most InstantSearch components connect to the Searcher.
To configure the searcher and link it to the interface:
- Instantiate the searcher.
Create aHitsSearcherinstance with the application ID, API key, and index name. - Add the connector property.
Add asearchConnectorproperty to the view controller.
Connectors in InstantSearch establish links between components. - Initialize the connector.
Initialize theSearchConnectorwith theSearcher,searchController,hitsInteractor, andhitsControlleras parameters. - Activate the connector and start the search.
Call the connector’sconnect()method to activate it.
Addsearcher.search()to theviewDidLoad()method to launch the first empty search request.
Swift
setupUI method to the view controller and call it from the viewDidLoad method.
Finally, override the viewDidAppear method,
and set search controller so that the search controller presents results immediately after the view controller appearance.
Swift

Adding statistics
To make the search experience more user-friendly, it would be appropriate to provide an additional feedback about the search results. This is an opportunity to extend the search experience with different InstantSearch modules. First, add a statistics component. This component will show the hit count. This gives users a complete understanding of their search, without the need for extra interaction. Then, instantiate aStatsInteractor, which extracts the required metadata from the search response, and provides an interface to present it to users.
Swift
StatsInteractor receives the search statistics now, but it isn’t displayed anywhere yet. To keep the example simple, the hits count will be presented as the title of the view controller. This is probably not the best place to show this in the application interface, but this prevents putting to much layout-related code in this guide.
The StatsInteractor presents its data in a component that implements the StatsTextController protocol.
Make the view controller conform to this protocol by adding an extension. Now, the view controller can be connected to the StatsInteractor with the corresponding method. Add this connection in the viewDidLoad method.
Swift

- Each module has an
Interactor, containing the module’s business-logic. - Each
Interactorhas a correspondingControllerprotocol, which defines the interaction with a UI component.
Controller protocol for UIKit components.
Examples of these are HitsTableViewController, TextFieldController, and ActivityIndicatorController.
Feel free to use them to discover the abilities of InstantSearch with minimal effort.
In your own project, you might want implement more custom UI and behavior.
If so, it’s up to you to create an implementations of the Controller protocol, and to connect them to a corresponding Interactor.
Filter your results: RefinementList
With your app, you can search more than 10,000 products. However, you don’t want to scroll to the bottom of the list to find the exact product you’re looking for. To resolve it, build a filter that allows to filter products by their category using the RefinementList components.
First, add a FilterState component. This component provides a convenient way to manage the state of your filters. In this example, a refinement attribute: category will be added. Finally, add the RefinementList components to other components in the search experience, such as FacetListConnector, FacetListTableController and UITableViewController. The UITableViewController will actually present a facet list. As a result, the definition of your ViewController has to look like this:
Swift
setupUI() method, set up a navigation bar button item that triggers the presentation of the facet list,
and sets the title of this list.
Add showFilters and dismissFilters functions responsible for the presentation and dismiss logic of the facet list.
Swift
RefinementList in action.


Going further
Your users can enter a query, and your application shows them results as they type. It also provides a possibility to filter the results even further usingRefinementList.
That is pretty nice already, but you can go further and improve on that.
- You can have a look at the examples to see more complex examples of applications built with InstantSearch.
- You can head to the components page to see other components that you could use.