Watson Developer Cloud Swift SDK
Deprecation Notice
This repo is being deprecated. While this repo will no longer see active support from the IBM team, contributions by the community via PRs are still welcomed and will be reviewed. Keep in mind, updates will no longer be published to spm, Carthage, or CocoaPods and changes made after this update will need to be built independently.
Overview
The Watson Developer Cloud Swift SDK makes it easy for mobile developers to build Watson-powered applications. With the Swift SDK you can leverage the power of Watson's advanced artificial intelligence, machine learning, and deep learning techniques to understand unstructured data and engage with mobile users in new ways.
There are many resources to help you build your first cognitive application with the Swift SDK:
Services
This SDK provides classes and methods to access the following Watson services.
Announcements
Tone Analyzer Deprecation
As of this major release, 5.0.0, the Tone Analyzer api has been removed in preparation for deprecation. If you wish to continue using this sdk to make calls to Tone Analyzer until its final deprecation, you will have to use a previous version.
On 24 February 2022, IBM announced the deprecation of the Tone Analyzer service. The service will no longer be available as of 24 February 2023. As of 24 February 2022, you will not be able to create new instances. Existing instances will be supported until 24 February 2023.
As an alternative, we encourage you to consider migrating to the Natural Language Understanding service on IBM Cloud. With Natural Language Understanding, tone analysis is done by using a pre-built classifications model, which provides an easy way to detect language tones in written text. For more information, see Migrating from Watson Tone Analyzer Customer Engagement endpoint to Natural Language Understanding.
Natural Language Classifier Deprecation
As of this major release, 5.0.0, the NLC api has been removed in preparation for deprecation. If you wish to continue using this sdk to make calls to NLC until its final deprecation, you will have to use a previous version.
On 9 August 2021, IBM announced the deprecation of the Natural Language Classifier service. The service will no longer be available from 8 August 2022. As of 9 September 2021, you will not be able to create new instances. Existing instances will be supported until 8 August 2022. Any instance that still exists on that date will be deleted.
As an alternative, we encourage you to consider migrating to the Natural Language Understanding service on IBM Cloud that uses deep learning to extract data and insights from text such as keywords, categories, sentiment, emotion, and syntax, along with advanced multi-label text classification capabilities, to provide even richer insights for your business or industry. For more information, see Migrating to Natural Language Understanding.
Before you begin
Requirements
- Xcode 10.2+
- Swift 5.0+
- iOS 10.0+
Installation
The IBM Watson Swift SDK can be installed with Swift Package Manager, Cocoapods, or Carthage.
Swift Package Manager
New in version 4.0.2, the Watson Developer Cloud Swift SDK now supports all services through Swift Package Manager.
On the XCode menu bar at the top of your screen click File -> Swift Packages -> Add Package Dependencies, follow the prompts by pasting the github url https://github.com/watson-developer-cloud/swift-sdk and using the most recent major version if appropriate. Make sure to only click on the services you plan on using otherwise you may face long build times
To import a service into your project:
import AssistantV2
import DiscoveryV2
.
.
.
(Speech To Text and Text To Speech only)
The use of the libogg and opus libraries by these services requires extra steps to be taken BEFORE the package is installed.
You will need Homebrew installed
Install libogg and opus
$ brew install libogg opus
Packaged dynamic libraries must be removed according to current library versions.
libogg version as of writing: 1.3.4.
opus version as of writing: 1.3.1
$ rm -f /usr/local/Cellar/libogg/1.3.4/lib/*.dylib
$ rm -f /usr/local/Cellar/opus/1.3.1/lib/*.dylib
The static libraries installed must be replaced with libraries compiled for multiple architectures. These libraries can be downloaded from this github repo for libogg here and opus here
Replace the currently installed libogg and libopus libraries
rm -f /usr/local/Cellar/libogg/1.3.4/lib/libogg.a && cp ~/Downloads/libogg.a /usr/local/Cellar/libogg/1.3.4/lib
rm -f /usr/local/Cellar/opus/1.3.1/lib/libopus.a && cp ~/Downloads/libopus.a /usr/local/Cellar/opus/1.3.1/lib
If you run into any build errors or imported the package before performing the above steps, the project may need to be reindexed. Remove the WatsonDeveloperCloud package from your XCode project file under Swift Packages; then, from the XCode menu bar on the top of the screen click Product -> Clean Build Folder and lastly reinstall the package.
You're ready to go!
Cocoapods
You can install Cocoapods with RubyGems:
$ sudo gem install cocoapods
If your project does not yet have a Podfile, use the pod init command in the root directory of your project. To install the Swift SDK using Cocoapods, add the services you will be using to your Podfile as demonstrated below (substituting MyApp with the name of your app). The example below shows all of the currently available services; your Podfile should only include the services that your app will use.
use_frameworks!
target 'MyApp' do
pod 'IBMWatsonAssistantV1', '~> 5.0.0'
pod 'IBMWatsonAssistantV2', '~> 5.0.0'
pod 'IBMWatsonDiscoveryV1', '~> 5.0.0'
pod 'IBMWatsonLanguageTranslatorV3', '~> 5.0.0'
pod 'IBMWatsonNaturalLanguageUnderstandingV1', '~> 5.0.0'
pod 'IBMWatsonSpeechToTextV1', '~> 5.0.0'
pod 'IBMWatsonTextToSpeechV1', '~> 5.0.0'
end
Run the pod install command, and open the generated .xcworkspace file. To update to newer releases, use pod update.
When importing the frameworks in source files, exclude the IBMWatson prefix and the version suffix. For example, after installing IBMWatsonAssistantV1, import it in your source files as import Assistant.
For more information on using Cocoapods, refer to the Cocoapods Guides.
Carthage
NOTE: The release of Apple's new M1 chip has caused issues in Carthage for XCode versions 11+. For the foreseeable future, we are unable to support XCode 11 through Carthage. We would recommend installing through Swift Package Manager (preferable) or upgrading to XCode 12 (where there is a workaround).
NOTE: Our frameworks cannot currently be run on the new Apple Silicon Macs through Carthage. Again, we recommend the use of Swift Package Manager instead.
You can install Carthage with Homebrew:
$ brew update
$ brew install carthage
If your project does not have a Cartfile yet, use the touch Cartfile command in the root directory of your project. To install the IBM Watson Swift SDK using Carthage, add the following to your Cartfile.
github "watson-developer-cloud/swift-sdk" ~> 5.0.0
Follow the remaining Carthage installation instructions for the XCode 12 workaround here. Then run the following command to build the dependencies and frameworks:
$ carthage.sh bootstrap --platform iOS
Note that the above command will download and build all of the services in the IBM Watson Swift SDK and does take awhile.
Follow the next steps to link the frameworks to your XCode project:
Make sure to drag-and-drop the built frameworks (only for the services your app requires) into your app target under General -> Frameworks, Libraries, and Embedded Content (XCode <= 10.x: General -> Linked Frameworks and Libraries ) and import them in the source files that require them. You will find the .framework files under ./Carthage/Build/iOS from your source directory.
The following frameworks need to be added to your app:
IBMSwiftSDKCore.framework
Whichever services your app will be using (AssistantV1.framework, DiscoveryV1.framework, etc.)
(Speech to Text only) Starscream.framework. Be sure to add this framework to your input.xcfilelist and output.xcfilelist which will be detailed below
XCode 12 only: Under the Embed column make sure each framework is set to Do Not Embed
On your application targets’ Build Phases settings tab, click the + icon and choose New Run Script Phase. Create a Run Script in which you specify your shell (ex: /bin/sh), add the following contents to the script area below the shell:
/usr/local/bin/carthage copy-frameworks
Create a file named input.xcfilelist and a file named output.xcfilelist
Add the paths to the frameworks you want to use to your input.xcfilelist. For example:
$(SRCROOT)/Carthage/Build/iOS/IBMSwiftSDKCore.framework
$(SRCROOT)/Carthage/Build/iOS/DiscoveryV1.framework
Add the paths to the copied frameworks to the output.xcfilelist. For example:
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/IBMSwiftSDKCore.framework
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/DiscoveryV1.framework
With output files specified alongside the input files, XCode only needs to run the script when the input files have changed or the output files are missing. This means dirty builds will be faster when you haven't rebuilt frameworks with Carthage.
Add the path to input.xcfilelist to the "Input File Lists" section of the Carthage run script phase. This will usually be $(SRCROOT)/input.xcfilelist
Add the path to output.xcfilelist to the "Output File Lists" section of the Carthage run script phase. This will usually be $(SRCROOT)/output.xcfilelist
If your app fails to build because it is built with a different version of Swift than the downloaded SDK, then re-run the carthage.sh bootstrap command with the --no-use-binaries flag added.
Authentication
In order to use an IBM Watson service in a Swift application, you will need to authenticate. The following describes the typical path you need to take to do so.
Step 1: Getting credentials
Credentials to use an IBM Watson service are obtained via IBM Cloud. You will need an active account and a service instance for the service that you wish to use prior to authenticating in your Swift app.
You can access the service credentials for your instance by taking the following steps:
- Go to the IBM Cloud Dashboard page.
- Either click an existing Watson service instance in your resource list or click Create resource > AI and create a service instance.
- Click on the Manage item in the left nav bar of your service instance.
On this page, you will see your credentials to use in the SDK to access your service instance.
Step 2: Authenticating in Code
The Watson Swift SDK manages authentication using an Authenticator class. There are multiple types of Authenticators depending on your preferred method.
WatsonIAMAuthenticator (most common)
The WatsonIAMAuthenticator