Disclaimer: IBM Cloud Tools for Swift (ICT) is no longer supported. You can find updated instructions for setting up the Cognitive Concierge App over on the github repo.

Swift is growing, and with it an ecosystem of tools

swift-bird 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 Cloud. 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.


screenshot 1screenshot 3screenshot 7


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 IBM Cloud.

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…

Setting up your Application

    1. Deploy the Server Application to IBM Cloud 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 IBM Cloud. Click Provision and Deploy Sample Server under Cloud Runtimes. Give your Cloud Runtime a unique name, and click Next.
    1. 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 IBM Cloud. 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.
    2. Update Conversation Service

Once ICT has provisioned your Cloud Runtime, you should have a Conversation Service instance in your IBM Cloud 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 IBM Cloud icon in Cloud Runtime to go to your ‘Application Details’ page on IBM Cloud.

      • 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

  1. 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 IBM Cloud using ICT, some services were provisioned for you, 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 IBM Cloud. 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 IBM Cloud which you can access by clicking on the 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 IBM Cloud 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!

6 comments on"Build an end-to-end Swift app with Watson APIs"

  1. Could this system be useful in helping to find and purchase birthday gifts or coupon codes? Our personalization and print submission software is written in Swift. We have some ideas.

    • Belinda Vennam January 03, 2017

      Using this as a starting point, you could certainly expand into a number of different useful apps! You would need to make some edits to the application code, as well as to the conversational backend, but this would be a great starting point to see how recommendations might be done in general. You could then expand to do the specific types of recommendations you would like to, whether that’s birthday gifts, coupon codes, or vacation suggestions. The sky is really the limit!

  2. Hi Belinda!
    I’m new with watson, and I try this steps but the project is up-to-dated to swift 2.3 and ConversationV1 (from sdk) is updated to latest (swift) version, so I’m unable to mix both versions (they cannot be imported). I’ve tried to change “Use Legacy Swift Language Version” key to NO from target, but then, others frameworks which uses swift 2.3 could not be imported. Then I tryed to update pods, but this give me more errors.. So, now I’m trying to create from scratch, i’m using 30 days trial services and I haven’t found in anyplace a step-by-step from what I really need to create a bot with watson to my app. Just this example, which (for me) its lot of confusing. There is better way to follow, where I’ll lost less time to understand and learn?
    Thank you!

  3. This is great. We’re a niche-specific development agency and think there could be great synergy for our enterprise customers. Do you think we could get some dev support to fast track the process? Thanks.

    • LeslieSKChau August 17, 2017

      Hi Harish, appreciate your feedback! Can you reach out to me on our Slack team? @lesliechau is my handle.

Join The Discussion

Your email address will not be published. Required fields are marked *