Replicating the iOS 11 App Store search tab

Lately I’ve been working with UISearchController for a friends open source app and decided to share the approach we took to replicate the App Store search functionality.

The idea was to categorize the search type and use a state container view controller to swap the resulting view controller depending on the search type.

Here’s an overview of the hierarchy:

Having our components split this way and using UISearchController means no handling of the current search state. We won’t have if filtered {} spread arround a massive view controller that changes the entire structure if the user types something on the searchbar.

Using SearchResultsController

Taking advantage of the implementation of UISearchControllersearchResultsController makes it magically to switch between our original content and our filtered one without ever handing a search status. This approach becomes even easier when displaying the same UI as we’re currently showing as you can reuse the UIViewController but this is not our case.

searchController=UISearchController(searchResultsController:resultsContainerViewController)// Selecting a suggestion termresultsContainerViewController.didSelect=search// Search bar delegate to determine the type of search being performedsearchController.searchBar.delegate=selfsearchController.searchBar.placeholder="Search"searchController.searchResultsUpdater=selfnavigationItem.searchController=searchControllernavigationItem.hidesSearchBarWhenScrolling=falsedefinesPresentationContext=true

Differentiating SearchType

enumSearchType{casepartialcasefinal}

Our flow can go two ways, while typing the ‘suggestion terms’ screen will pop up showing a few app recommendations you might be looking for. When you’re done and tap search or tap on a trending term or suggestion the apps screen will be displayed and fetch the apps matching that term.

We use the search(term: String) method when tapped on a term. This will define the searchType and activate the UISearchController.

Displaying results

Finally, since all our view controller swapping code is handled by the result view controller we only need to implement UISearchResultUpdating to perfom an action everytime the user types in the searchbar. updateSearchResults gets called everytime there is interaction with the searchController so it would be useful to handle those cases too.