- A list to display search results
- A search bar to type your query
- Statistics about the current search
- A facet list for filtering results
Prepare your project
To use InstantSearch iOS, you need an Algolia account. You can create a new account, or use the following credentials:- Application ID:
latency - Search API Key:
1f6fd3a6fb973cb08419fe7d288fa4db - Index name:
bestbuy
Create a new project
In Xcode, create a new Project: Open Xcode, and selectFile -> New -> Project in the menu bar.

iOS -> App template and click Next.

SwiftUI option in the Interface field and click Next.

ContentView.swift file opened with a Hello World project and the live preview canvas.

Add project dependencies
This tutorial uses Swift Package Manager to integrate the InstantSearch library. If you prefer to use another dependency manager (Cocoapods, Carthage) please checkout the corresponding installation guides for InstantSearch.
In the menu bar select File -> Swift Packages -> Add Package Dependency.

https://github.com/algolia/instantsearch-ios

InstantSearchSwiftUI product from the following list:

Implementation
Start by creating a classic search interface with search bar and results list. In your Xcode project, open theContentView.swift file and import the InstantSearch library.
Swift
Define your record structure
Define a structure that represent a record in your index. For simplicity’s sake, the structure only provides the name of the product. It must conform to theCodable protocol to work with InstantSearch. Add the following structure definition to the ContentView.swift file:
Swift
Business logic
Add theAlgoliaController class containing the InstantSearch business logic components to the ContentView.swift file.
You need three components coupled with the corresponding UI controllers for the basic search experience:
HitsSearcherperforms search requests and obtains search results.SearchBoxInteractorhandles a textual query input and triggers search requests when needed.HitsInteractorstores hits and manages the pagination logic.
setupConnections method establishes the connections between these components and their UI controllers to make them work together seamlessly.
Swift
UI
1
Declare observable controllers
Add
SearchBoxObservableController and HitsObservableController properties to the ContentView structure with an @ObservedObject property wrapper, so the view is automatically notified when the state of the search text or the hits list changed.Swift
2
Add state for search box editing
Add the
isEditing property that binds the editing state of the search box.Swift
3
Add search box to the view body
Replace the “Hello world!” text view with a vertical stack containing the From iOS 15, you can use the The body of your view will look as follows:The following steps rely on an explicit
SearchBar configured with SearchBoxObservableController properties and isEditing state binding.Swift
searchable modifier to add a system search field.Embed your search view into the
NavigationView to ensure the search input field appears.Swift
SearchBar view (as a more versatile approach).4
Display search results with a hits list component
Insert the
HitsList component configured with HitsObservableController and a closure constructing the hit row.
The hit row is represented by a vertical stack with a text block presenting the name of the item and a Divider.Swift
5
Add no results view and navigation title
Complete your search experience with a
noResults trailing closure in the HitsList, that constructs the view presented in case of an empty result set, and add the navigationBarTitle string to show the navigation header on top of you search screen.Swift
6
Connect business logic to the UI
Try out the search experience in the live preview.
Add a static instance of the
AlgoliaController in the PreviewProvider.
Then, in the previews declaration, instantiate the ContentView with the UI controller references in the AlgoliaController class, and embed it in the NavigationView.
Launch the initial search inside the onAppear closure of the NavigationView.
The resulting ContentView_Previews structure content should look as follows:Swift
Preview
Launch your preview to see the basic search experience in action. You should see that the results are changing on each key stroke.

Add statistics
To make the search experience more user-friendly, you can give more context about the search results to your users. You can do this with different InstantSearch modules.1
Connect a statistics controller
This component shows the hit count and the request processing time.
This gives users a complete understanding of their search, without the need for extra interaction.
Then create a
StatsInteractor, which extracts the metadata from the search response, and provides an interface to present it to users.
Add StatsInteractor and StatsTextObservableController to AlgoliaController and connect the StatsInteractor to the Searcher in the setupConnections method.
Complete the setupConnections method of the PreviewProvider with the connection between the StatsInteractor and the StatsTextObservableController.Swift
2
Show search statistics in the view
The
StatsInteractor receives the search statistics now, but doesn’t display it yet.Add StatsTextObservableController property to the ContentView and add the Text with its stats property into the stack in the middle of the SearchBar and the HitsList.Swift
3
Update preview with statistics controller
Alter the
PreviewProvider structure by adding a StatsTextObservableController in the ContentView initializer.Swift

Filter your results
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. One can more accurately filter the results by making use of theRefinementList components. This section explains how to build a filter that allows to filter products by their category.
- Add a
FilterStatecomponent to theAlgoliaController. This component provides a convenient way to manage the state of your filters. Add themanufacturerrefinement attribute. - Add the
FacetListInteractor, which stores the list of facets retrieved with search results. - Add the connections between
HitsSearcher,FilterStateandFacetListInteractorin thesetupConnectionsmethod. Complete thesetupConnectionsmethod of thePreviewProviderwith the connection between theFacetListInteractorand theFacetListObservableController. To improve the user experience, the connection includes theFacetListPresenterparameter that pins the selected facets to the top of the list and uses facet count value as the second ranking criteria, so that the facets with the most hits show up higher.
Swift
ContentView add a new state flag isPresentingFacets, which defines if the facet list is presented.
Swift
isPresentingFacets flag.
Swift
FacetListObservableController to the ContentView and a facets function constructing the facet list view.
Swift
ContentView code should look as follows:
Swift
ContentView with the FacetListObservableController.
Swift


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. This is a great start, but you can go even further and improve on that.
- Have a look at more complex examples of applications built with InstantSearch.
- Head to the InstantSearch components page to see other components that you could use.