After spending a long time in beta, the Swift SDK for IBM Watson is now officially released! This SDK provides an easy, intuitive way to integrate Watson’s AI platform into your iOS apps or Swift servers on Linux. Although you can access the Watson APIs with standard REST requests, this SDK abstracts away all of the underlying HTTP infrastructure (including authentication and JSON encoding/decoding), so that you can communicate directly with Watson services in a more “swifty” way.

Here is the list of services that the Swift SDK supports right now.

Setting up and installing

First, provision the services that you want to use on the IBM Cloud. For this example, let’s use Tone Analyzer to understand the emotional and social context of what your users are saying. After you’ve created your service instance, a set of credentials is created for you.

For an iOS app, you can install Tone Analyzer directly with Cocoapods, or install the entire SDK with Carthage. For Linux, use Swift Package Manager.

Configuring

Next, import the service at the top of your source files. The import statement differs depending on which dependency management tool you used.

Cocoapods: import ToneAnalyzer

Carthage: import IBMWatsonToneAnalyzerV3

Swift Package Manager: import ToneAnalyzerV3

Now, create an instance of the service you want to use:

Substitute the values for myApiKey and toneAnalyzer.serviceURL using the API Key and URL obtained from the service instance you created (see the earlier screenshot for reference). The version parameter specifies the minor version of the backend API, which is explained further in the API reference.

Using the APIs

Now comes the fun part! You can use the instance of the Watson service created in the previous step to call any of the supported API methods. Let’s use our ToneAnalyzer service to see how customers are feeling based on text they use within the app.