Create Swift mobile apps with IBM Watson services

Create Swift mobile apps with IBM Watson services

Course Features

Course Details

Course overview

Learn how to write three mobile apps in the Swift language on iOS that use the IBM® Watson™ Cloud Developer SDK to access the Watson service. Learn, also, how to write simple but cool cognitive applications that use the following Watson services:
  • AlchemyLanguage (Watson Natural Language Understanding) A collection of APIs that provide text analysis by processing natural language. For this course, you’ll use the Sentiment Analysis service to identify the sentiment in text.
  • Visual Recognition Analyzes images for scenes, objects, people, signs, and other content. With this service, you’ll provide a URL to an image that your application will identify.
  • Text to Speech Synthesizes natural-sounding speech from input text in a variety of languages and voices that speak with appropriate cadence and intonation.
IBM Watson Cognitive services, which are hosted on IBM Bluemix (IBM Cloud), can be accessed through RESTful API calls. You’ll learn how to call these services from your Swift application.
Get started

Lab 1: analyze sentiment

FREE
Create a typical application in Swift
3 of 9
You’ll build a simple application with a submit button, an editable text field, and an output field. Create a simple single view application with a graphical user interface (GUI) that includes a text field, a label, and a button. When a user presses the button, the URL in the text field is sent to Watson, which analyzes it and returns an opinion that is shown in the output field. Take the text in the text field and echo it in the label field when the button is pressed.   See the Xcode documentation  for information about how to create basic GUIs in Xcode. Connect the GUI in Main.storyboard to the code in the ViewController file: Double-click TextField (by pressing the mouse pad with two fingers), which contains the URL to the document http://www.huffingtonpost.com/2010/06/22/iphone-4-review-the-worst_n_620714.html. Select New Referencing Outlet from the list. Enter textField in the dialog. Insert the new reference between class ViewController and override func viewDidLoad. Click Connect. The inserted text is @IBOutlet weak var textField: UITextField! Connect the button. Insert the sent event named Touch up Inside before the last curly bracket. The inserted text is: @IBAction func checkButtonPressed(sender: AnyObject) { } Connect the text label. Insert the parameter UILabel between @IBOutlet weak var textField: UITextField! and override func… The inserted text is @IBOutlet weak var textStatusLabel: UILabel! Test the code by adding NSLog(textField.text!) to the end of the checkButtonPressed method. NSLog allows you to log your actions. Setting textStatusLabel.text with textField.text allows you to show the entered text of the Text field in the Label field. import UIKit class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! @IBOutlet weak var textStatusLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func checkButtonPressed(sender: AnyObject) { NSLog(textField.text!) //checking sentiment //setting feedback on sentiment textStatusLabel.text =...

Continue Reading
FREE
Install Carthage and add the Watson SDK to your project
4 of 9
Install the Carthage dependency manager. Use the Carthage dependency manager to install libraries that are used by your application. You can install Carthage from GitHub or use Homebrew to update it only if you use Xcode 7.x. Important: If you previously installed the binary version of Carthage, you must delete /Library/Frameworks/CarthageKit.framework. To install Carthage from GitHub, download and run the latest release of the Carthage.pkg file: https://github.com/Carthage/Carthage/releases. Then follow the installation prompts. To install Carthage by using Homebrew (only on Xcode 7.x), run the commands brew update and brew install Carthage. If you want to run the latest development version of Carthage, which might be highly unstable or incompatible, clone the master branch of the repository and then run the command make install. Important: If you have two-factor authentication, Carthage requires an OTP header. Add the Watson SDK to your project Open a Bash shell in the root directory of your project. Enter cat > cartfile Enter github "watson-developer-cloud/ios-sdk” Enter a new line. Then, press control + C to exit Edit mode. From the command line at the root of the project, run the command carthage update --platform iOS. If you receive a compile failure for the framework AlamofireObjectMapper, run the command again. The following image shows output from the package manager as it fetches and builds iOS library components: Add the SDK to the Xcode project: Under the project name, create a new group in your Xcode project called Framework. Navigate to the root folder of your project in Xcode and select all the framework files in the /Carthage/Build/iOS/ directory of your project (AlchemyLanguageV1.framework, Alamofire, Freddy, RestKit, Starscream). Drag and drop those files from Finder into the new Framework group in your project in Xcode. Be sure you clear the option to copy items. By not copying the items, you create only a reference to those Framework files. In the Build Phases tab,...

Continue Reading
FREE
Create the Watson service and get the key token for it
5 of 9
Create the Watson AlchemyAPI service. From the IBM Bluemix catalog, click Watson > AlchemyAPI > Create. Be sure to use a static API key as shown in the following image. Find and copy the Service Credentials. Bluemix provides your credentials in JSON format. The JSON snippet lists credentials, such as the API key and secret, and connection information for the service. Add the following lines of code to call the cognitive service. Insert the import field under previously existing import fields. Import field: import AlchemyLanguageV1 So far, your code should look like this: //checking sentiment let apiKey = "" let alchemyLanguage = AlchemyLanguage(apiKey: apiKey) //alchemyLanguage. let url = textField.text! //"http://www.huffingtonpost.com/2010/06/22/iphone-4-review-the-worst_n_620714.html" let failure = { (error: NSError) in print(error) } NSLog("calling GetTextsentiment url :::::::::::::") alchemyLanguage.getTextSentiment(forURL: url, failure: failure) { sentiment in print(sentiment) NSLog((sentiment.docSentiment?.type)!) //setting feedback on sentiment self.textStatusLabel.text = "text status sentiment ::::::::::::: " + (sentiment.docSentiment?.type)! } You can ignore any warning messages including the message about “var textData was never mutated.” Run the application. You should see the following results after the application checks the sentiment of some text: This is the complete code for the application: import UIKit import AlchemyLanguageV1 class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! @IBOutlet weak var textStatusLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func checkButtonPressed(sender: AnyObject) { NSLog(textField.text!) //checking sentiment let apiKey = "" let alchemyLanguage = AlchemyLanguage(apiKey: apiKey) //alchemyLanguage. let url = textField.text! //"http://www.huffingtonpost.com/2010/06/22/iphone-4-review-the-worst_n_620714.html" let failure = { (error: NSError) in print(error) } NSLog("calling GetTextsentiment url :::::::::::::") alchemyLanguage.getTextSentiment(forURL: url, failure: failure) { sentiment in print(sentiment) NSLog((sentiment.docSentiment?.type)!) //setting feedback on sentiment self.textStatusLabel.text = "text status sentiment ::::::::::::: " + (sentiment.docSentiment?.type)! } } }

Lab 2: recognize images

FREE
Create a typical iOS application in Swift
3 of 8
You’ll build a simple application with a Submit button, an editable text field, and an output label field. Create a simple single view application with a graphical user interface (GUI) that includes a text field, a label, and a button. When the user presses the button, the URL address in the text field is sent to Watson, which analyzes it and returns an opinion that is shown in the output field. See the Xcode documentation for information about how to create basic GUIs in Xcode. Connect the GUI in Main.storyboard to the code in the ViewController file: Double-click TextField (by pressing the mouse pad with two fingers), which contains the URL. Enter this example text: https://pbs.twimg.com/media/Cb_ComqWAAEwbab.jpg Select New Reference Outlet from the list. Then, enter urlText in the dialog. Insert the new reference between class ViewController and override func viewDidLoad. Click Connect. The inserted text is @IBOutlet weak var urlText: UITextField! Connect the button from the Event Touch Up Inside below the function didReceiveMemoryWarning. Enter this text in the dialog: analysisButtonPressed The inserted text is: @IBAction func analysisButtonPressed(sender: AnyObject) { } Connect the text label. The result is @IBOutlet weak var analysisTextLabel: UILabel! Test the code by adding NSLog("url: "+urlText.text!) to the end of the analysisButtonPressed method. NSLog allows you to log your actions. Add analysisTextLabel.text = urlText.text! Setting analysisTextLabel.text with urlText.text allows you to show the output from the Text field in the Label field in the UI. import UIKit class ViewController: UIViewController { @IBOutlet weak var urlText: UITextField! @IBOutlet weak var analysisTextLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func analysisButtonPressed(sender: AnyObject) { NSLog("button pressed") NSLog("url: "+urlText.text!) analysisTextLabel.text = urlText.text! //call service //get output } } Build and execute your application.
FREE
Install Carthage and add the Watson SDK to your project
4 of 8
Because you will use a separate project for this application, you must again install the Carthage dependency manager and the SDK, and then add that SDK to your Xcode project. Install the Carthage dependency manager Use the Carthage dependency manager to install libraries that are used by your application. You can install Carthage from GitHub or use Homebrew to update it only if you use Xcode 7.x. Important: If you previously installed the binary version of Carthage, you must delete /Library/Frameworks/CarthageKit.framework. To install Carthage from GitHub, download and run the latest release of the Carthage.pkg file: https://github.com/Carthage/Carthage/releases. Then follow the installation prompts. To install Carthage by using Homebrew (only on Xcode 7.x), run the commands brew update and brew install Carthage. If want to run the latest development version of Carthage, which might be highly unstable or incompatible, clone the master branch of the repository and then run the command make install. Install the Watson SDK: Open a Bash shell in the root directory of your project. Enter cat > cartfile Enter github "watson-developer-cloud/ios-sdk" Enter a new line. Then, press control + C to exit Edit mode. From the command line at the root of the project, run the command carthage update --platform iOS. If you receive a compile failure for the framework AlamofireObjectMapper, run the command again. The following image shows output from the package manager as it fetches and builds iOS library components: Add the SDK to the Xcode project: Under the project name, create a new group in your Xcode project called Framework. Navigate to the root folder of your project in Xcode and select all the framework files in the /Carthage/Build/iOS/ directory of your project (VisualRecognitionV3, Alamofire,, Freddy, RestKit, Starscream). Drag and drop those files from Finder into the new Framework group in your project in Xcode. Be sure you clear the option to copy items. By not copying...

Continue Reading

Lab 3: convert text to speech

FREE
Create an application in Swift
3 of 7
You’ll build a simple application with a Submit button and an editable text field. There is no output field because the output is going to be delivered through voice. Create a simple single view application with a graphical user interface (GUI) that includes a text field, a segmented control and a button. When a user presses the button, the text in the text field is sent to Watson, which analyzes it and returns a voice file (in the provided voice). This will be a single view application. You take the text in the text field and simply log it in the console when the button is pressed. See the Xcode documentation for information about how to create basic GUIs in Xcode. Connect the GUI in Main.storyboard to the code in the ViewController file: Double-click TextField (by pressing the mouse pad with two fingers), which contains the text “Hello this is Watson speaking.” Select New Reference Outlet from the list. In the dialog, enter speakText. Insert the new reference between class ViewController and override func viewDidLoad. Click Connect. The result is @IBOutlet weak var speakText: UITextField! Connect the button by entering speakButtonPressed in the dialog. The inserted text is: @IBAction func speakButtonPressed(sender: AnyObject) { } Test the code by adding NSLog(speakText.text!) to the end of the checkButtonPressed method. Right-click the Segmented Control, which contains three controls on the right pane in the Attributes Inspector, and increase the number of segments to 3. Override the previous titles with Watson and Allyson respectively, and add the title “Kate” to the segment number 2. Double-click to select New Reference Outlet from the drop-down list. In the dialog, enter voiceSegment and insert it between Class View Controller and override func viewDidLoad. Click Connect. The result is: @IBOutlet weak var voiceSegment: UISegmentedControl! NSLog allows you to log your actions on the console. The code should look like this: Import UIKit class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! @IBOutlet weak var voiceSegment:...

Continue Reading
FREE
Install Carthage and add the Watson SDK to your project
4 of 7
As in the previous two labs, you must again install the Carthage dependency manager, the SDK, and then add that SDK to your Xcode project. Use the Carthage dependency manager to install libraries that are used by your application. You can install Carthage from GitHub or use Homebrew to update it only if you use Xcode 7.x. Important: If you previously installed the binary version of Carthage, you must delete /Library/Frameworks/CarthageKit.framework. To install Carthage from GitHub, download and run the latest release of the Carthage.pkg file: https://github.com/Carthage/Carthage/releases. Then follow the installation prompts. To install Carthage by using Homebrew (only on Xcode 7.x), run the commands brew update and brew install Carthage. If want to run the latest development version of Carthage, which might be highly unstable or incompatible, clone the master branch of the repository and then run the command make install. Install the Watson SDK: Open a Bash shell in the root directory of your project. Enter cat > cartfile Enter github "watson-developer-cloud/ios-sdk” Enter a new line. Then, press control + C to exit Edit mode. From the command line at the root of the project, run the command carthage update --platform iOS. If you receive a compile failure for the frameworks, run the command again. The following image shows output from the package manager as it fetches and builds iOS library components: Add the SDK to the Xcode project: Under the project name, create a new group in your Xcode project called Framework . Navigate to the root folder of your project in Xcode and select all the framework files in the /Carthage/Build/iOS/ directory of your project (TextToSpeechV1.framework, Alamofire, Freddy, RestKit, Starscream). Drag and drop those files from Finder into the new Framework group in your project in Xcode. Be sure you clear the option to copy items. By not copying the items, you create only a reference to those...

Continue Reading

Summary

More Courses by this Instructor