Create the Watson service and get the key token for it

Create the Watson AlchemyAPI service.

  1. From the IBM Bluemix catalog, click Watson > AlchemyAPI > Create. Be sure to use a static API key as shown in the following image.
  2. 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.
  3. Add the following lines of code to call the cognitive service. Insert the import field under previously existing import fields.
    Import field:
    import AlchemyLanguageV1So 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.

  4. 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)!

    }
    }
    }