algolia / instantsearch-ios-examples

Example apps built with InstantSearch iOS
https://community.algolia.com/instantsearch-ios/
MIT License
66 stars 19 forks source link

Install InstantSearchClient via SPM #20

Closed alelordelo closed 2 years ago

alelordelo commented 3 years ago

I am following this great tutorial: https://medium.com/@soares.rfarias/how-to-set-up-firestore-and-algolia-319fcf2c0d37

The author installs InstantSearchClient via CocoaPods. On my case, I would like to use SPM but didn't find InstantSearchClient package, only InstantSearch.

When I install InstantSearch via SPM, the code bellow doesn't work so I guess I need to install InstantSearchClient?

import Foundation
import InstantSearchClient

class MyViewModel {

    var myObjects = [MyObject]()
    private var collectionIndex : Index!
    private let query = Query()

    init() {
        setupAlgoliaSearch()
    }

    private func setupAlgoliaSearch() {
        guard let bundle = Bundle.main.bundleIdentifier else { return }
        let searchClient = Client(appID: "", apiKey: "")
        let indexName = bundle == "YOUR_PRODUCTION_BUNDLE" ? "COLLECTION_prod" : "COLLECTION_dev"
        collectionIndex = searchClient.index(withName: indexName)
        query.hitsPerPage = 20

        // Limiting the attributes to be retrieved helps reduce response size and improve performance.
        query.attributesToRetrieve = ["property1", "property2", "property3"]
        query.attributesToHighlight = ["property1", "property2", "property3"]
    }

    func searchCollection(forText searchString : String) {
        query.query = searchString
        collectionIndex.search(query) { (content, error) in
            guard let content = content else {
                if let error = error {
                    print(error.localizedDescription)
                }
                return
            }

            guard let hits = content["hits"] as? [[String: AnyObject]] else { return }

            hits.forEach({ (record) in
                // ... handle record JSON.
                let myObject = MyObject(json: record)
                myObjects.append(myObject)
            })
        }
    }
}
VladislavFitz commented 3 years ago

Hi @alelordelo ,

This guide is outdated. At the moment we provide Swift API client and InstantSearch iOS framework. Both of these libraries support Swift Package Manager, just copy-paste the repository URL.