The Swift API client
Algolia’s Swift API client lets you use Algolia’s APIs from your Swift app. Compared to using the APIs directly, using the Swift API client has these benefits:
-
Network retry strategy. The API client automatically retries connecting to another Algolia server, if the connection fails. Thanks to that, using the API clients is covered by Algolia’s SLA.
-
Reindexing. The API clients let you reindex your records in a single operation.
Algolia’s Swift API client is open source. Find the source code on GitHub.
Install the Swift API client
The Algolia Swift API client requires Swift version 5.0 or later and supports iOS, macOS, tvOS, and watchOS platforms.
Install via Swift Package Manager
To add the API client to your Swift project, follow these steps:
- Open your project in Xcode.
-
Go to File > Add Packages. In the search, enter the URL to the GitHub repository of the API client:
Copy1
https://github.com/algolia/algoliasearch-client-swift
Select the package
algoliasearch-client-swift
and click Copy Dependency. -
If you have a
Package.swift
file, add the repository as a dependency:Copy1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
let package = Package( dependencies: [ .package( url: "https://github.com/algolia/algoliasearch-client-swift", from: "8.0.0" ) ], targets: [ // Add the client as dependency to your targets .executableTarget( name: "algolia-example", dependencies: [ .product( name:"AlgoliaSearchClient", package: "algoliasearch-client-swift" ) ] ) ] )
Install via CocoaPods
To install the Algolia Swift API client with CocoaPods, add the following line to your Podfile
:
1
pod 'AlgoliaSearchClient', '~> 8.0'
Then, run the following command:
1
pod update
Install via Carthage
To install the Algolia Swift API client with Carthage, add the following line to your Cartfile
:
1
github "algolia/algoliasearch-client-swift" ~> 8.0.0
In your project directory, run these commands:
1
2
3
carthage update
./Carthage/Checkouts/algoliasearch-client-swift/carthage-prebuild
carthage build
Test your installation
If you haven’t already, create an Algolia account and create a new Algolia app to get started.
To test whether you can connect to Algolia, run a simple program that adds a record to a new index, searches the index, and print the results.
-
Copy the following code in your text editor. If you’re signed in, the code samples below show your Algolia application ID. If you’re not signed in, replace
YourApplicationID
with your Algolia application ID andYourWriteAPIKey
with your Write API Key. You can find both in your Algolia account.Copy1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// main.swift import AlgoliaSearchClient // A simple record for your index struct Record: Encodable { let objectID: ObjectID let name: String } // Add the client to the dependencies of your targets let client = SearchClient(appID: "YourApplicationID", apiKey: "YourWriteAPIKey") let index = client.index(withName: "test_index") // Create a new index and add a record let record: Record = .init(objectID: "1", name: "test_record") let indexing: ()? = try? index.saveObject(record).wait() // Search the index and print the results let results = try index.search(query: "test_record") print(results.hits[0])
-
Build and run the project:
Copy1
swift run
-
If the program ran successfully, you should see:
Copy1 2 3 4
Hit<JSON>(objectID: 1, object: { name: "test_record", objectID: "1", }
You can inspect your index now in the Algolia dashboard.
Next steps
Now you can interact with the Algolia Search API, you can look at the available methods, for example, for search or indexing.
Other APIs, for example for Algolia Recommend or Analytics, come with their own clients. To get an overview, see Initialize the API client.