Swift is growing, and with it an ecosystem of tools
As Swift continues to grow as a language, I’m excited about participating in and benefiting from the continued growth of the Swift ecosystem. The Swift sandbox enables me to run server side code in real time without the need to set up my own server. IBM’s Cloud Tools for Swift enables me to simplify the management of my Swift cloud deployments. There’s also the Swift Package Manager (newly released with Swift 3.0) for managing the distribution of Swift code in my projects. Finally, I can quickly and easily consume many of the cognitive capabilities through Watson Developer Cloud’s iOS SDK — whether in my client or server side Swift application!
Recently, IBM’s Mobile Innovation Lab created a starter application combining end-to-end Swift development with a few of the different Watson services provided on IBM’s Bluemix. The application, Cognitive Concierge, makes recommendations to the user based on a conversation with the app’s chat bot. You can find the open source code for the app on github.
The iOS client application is written in Swift and utilizes the Watson Developer Cloud’s iOS SDKs for Conversation, Text to Speech, and Speech to Text. The server side application is also written in Swift with the Kitura web framework and utilizes the Alchemy Language APIs. Consuming these cognitive services was made very simple by hosting our server side application on Bluemix.
There are a number of different reasons you might want a server side component for your iOS application. In our case, we were able to reduce networking calls and client side processing by pushing the restaurant recommendation code out to the server. If this app were in production, we would also have the option to easily update and improve the restaurant recommendations over time, without making changes to our client side code and requiring application updates for our users. Finally, by providing the restaurant recommendations code as a Swift API, we can easily share this API with other applications or application developers. With an API Management product like IBM’s API Connect, we could implement rate limiting and tiered levels for other developers that might want to build around our API.
This application could be used as a starting point for your own Cognitive + Swift application, or as an example for how you might set up a similar application yourself. To recreate this application on your own machine, follow the tutorial below.
Before you Begin…
- Sign up for IBM Bluemix.
- Download IBM Cloud Tools for Swift.
- Obtain a Google Places API Key, and enable it for Web and iOS.
- Install Carthage & Cocoapods dependency managers for iOS apps.
Setting up your Application
Deploy the Server Application to Bluemix using IBM Cloud Tools for Swift (ICT).
- Open the IBM Cloud Tools for Swift MacOS Application.
- Click the Create (+) button to set up a new project. Next, select the Cognitive Concierge Quickstart Application.
- Click Save Files to Local Computer to clone the project.
- Once the project is cloned, open up the .xcodeproj file that was created for you in ICT under Local Server Repository. Edit the Sources/restaurant-recommendations/Configuration.swift file’s Constants struct with your own Google API Key for Web.
- Finally, you can use ICT to deploy the server on Bluemix. Click Provision and Deploy Sample Server on Bluemix under Cloud Runtimes. Give your Cloud Runtime a unique name, and click Next.
Point the iOS Application to the Server Application
- With ICT, you can control whether the mobile application is pointing to a local or remote instance of your server code. For this step, ensure that the Connected to: field in the Client application settings is pointing to your server instance running on Bluemix. You can also point to your localhost for local testing, but you need to be running a local instance of the server application for this to work.
Update Conversation Service
Once ICT has provisioned your Cloud Runtime, you should have a Conversation Service instance in your Bluemix dashboard. This Service enables you to add a natural language interface to your applications. While you could create a conversation tree manually, we’ve provided the conversation for this application in the Resources folder at the top level of the project.
- In ICT, Click on the Bluemix icon in Cloud Runtime to go to your ‘Application Details’ page on Bluemix.
- Select the CognitiveConcierge-Conversation Service under Connections.
- Scroll down and select Launch Tool.
- Log into Watson Conversation with your IBM ID and you will be brought to the ‘Create Workspace’ page. Select Import, and upload your .JSON file (Resources/conversationWorkspace.json) representing the conversation for this application.
- Once the Conversation is created, select the More Options icon, and click View Details. Note the Workspace ID for use in your iOS application.
- Now go to the Client App section of the project and click on the icon next to the label containing CognitiveConcierge.xcworkspace. Copy the WorkspaceID into the CognitiveConcierge.plist file.
Note: Watson may take a few minutes to train based on the conversation workspace that you just uploaded. You can see if Watson is finished training by clicking the conversation workspace, then Dialog, and then the chat icon in the upper right hand corner. This is also how you can try out the conversation and test your bot. Run the iOS Application
Install Dependencies and run the iOS app
- From Terminal, change directories into the YourProjectName/CognitiveConcierge-iOS folder.
- Run the following commands to install the necessary dependencies:
carthage update --platform iOS pod install
- Open the CognitiveConcierge.xcworkspace file (your Client App in ICT) in Xcode 8.2
- When you deployed the server application to Bluemix using ICT, some services were provisioned for you on Bluemix, including Conversation, Text to Speech, and Speech to Text. Each of these services has some credentials that are required for the iOS application to be identified by your services on Bluemix. Put each of the required credentials into the CognitiveConcierge.plist file found at CognitiveConcierge-iOS/CognitiveConcierge/CognitiveConcierge.plist.
<To find the credentials follow the below steps:
- Return to your application details page on Bluemix which you can access by clicking on the Bluemix icon within ICT’s Cloud Runtime.
- Click on ‘Runtime’ then ‘Environment Variables’ to access every services’ credentials within VCAP_Services to add into the CognitiveConcierge.plist file.
- Press the Play button in Xcode to build and run the project in the simulator or on your iPhone!
Find a Restaurant with CognitiveConcierge
Now that the application is running, select the Restaurants option, which is the only option currently implemented for this starter app. This will bring you to the chat screen powered by Watson’s Conversation service. You could look for restaurant options for your birthday, anniversary, or just a date night. All of these are defined in the Conversation service’s conversation workspace file you previously uploaded to the Conversation service. Once the conversation bot has determined the occasion and time for your upcoming event, the iOS app makes a call to the server side Swift restaurant recommendations API. This API analyzes restaurant review text from the Google Places API with the Alchemy Language API to provide the perfect restaurant choices for you. These options are then returned to the iOS application for display.
To see these server calls in action, you could run the server application locally in Xcode by opening the Server .xcodeproj file (restaurant-recommendations.xcodeproj), changing the scheme to the executable, and then pressing play.
Place a breakpoint in the main.swift file at the endpoint for /api/v1/restaurants. When you run the iOS application and complete your conversation, you’ll see the breakpoint in your local server getting hit.
You have now successfully created both an iOS application and server side application in Swift. You have also connected those two applications to the appropriate APIs and services on Bluemix or elsewhere. We would love to see what extensions you create by building on top of this application. We are also happy to receive any feedback you’d be willing to share!