Skip to content

Using Online API to make Network Requests

Arpillai edited this page Dec 15, 2017 · 8 revisions

The goal of this guide is to show you how to use the Predix SDK For iOS Online API to make network requests to a Predix Cloud application or service.

Prerequisites

You must have:

  • Completed all tasks from our Getting Started Guide
  • Completed all tasks from our Authentication API Guide
  • Knowledge of Xcode's Interface Builder
  • Experience in connecting the Xcode Interface Builder elements (IBOutlets and IBActions) to a Swift UIViewController

Retrieving a Bearer Token from UAA

Using the Authentication API Guide as a base, we are going to bring in the Authentication Manager and Views from the Authentication project to bring up an authentication screen. The authentication screen allows us to simply press a button and send a request to a Predix Service without requesting for a UAA bearer token.

Note - Since we are referencing the Authentication API Guide example for authentication, we strongly recommend that you read this guide and complete the tasks outlined in this guide before you start working on the following tasks:

Adding a New View Controller

  1. Open your project and create a new UIViewController.

    File > New > Cocoa Touch Class

  2. Enter Class: name as OnlineAPIViewController and select the UIViewController from the Subclass of: drop down.

  3. Navigate to your main StoryBoard and drag in a new View Controller.

Add View Controller

  1. Modify your View Controller to include a Custom Class that matches the name of the UIViewController that you created in Step 2.

Update View Controller class

  1. Add a button and two labels as shown below.

Add content

  1. Select the login view from the StoryBoard and add manual segues that connects to the View Controller.

manual segue

  1. Select the StoryBoard Segue and enter a name in the Identifier field. For this example, we are using the name, onlineAPI.

add segue identifier

  1. Add your new View Controllers, IBOutlets and IBAction to your UIViewController.

Wire up controller

Making an Online API Call

  1. In your newly created IBAction add the following code:
@IBAction func sendOnlineRequest(_ sender: Any) {
    let url = URL(string: "https://predixsdkhelloapi.run.aws-usw02-pr.ice.predix.io/hello")!
    self.statusLabel.text = "sending request to \(url.absoluteString)"
    let task = URLSession.predix.dataTask(with: url) { (data, response, error) in
        DispatchQueue.main.async {
            if let urlResponse = response as? HTTPURLResponse {
                self.statusLabel.text = "got a response from \(url.absoluteString): statusCode: \(urlResponse.statusCode)"
            } else {
                self.statusLabel.text = "got a response"
            }
                
            if let requestError = error {
                self.statusLabel.text = "bad response from request: \(requestError)"
                return
            }
                
            if let jsonData = data, let json = try? JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions(rawValue: 0)) {
                self.responseLabel.text = "\(json)"
            } else {
                self.statusLabel.text = "could not parse response data"
            }            
        }
    }
        
    task.resume()
}
  1. Run the application and when prompted login with the user name demo and the password demo
  2. Press the send request button. If the call is successful, you will see a statusCode:200 in the Status Label and a response body.

Example of a successful request:

Status label:

got a response from https://predixsdkhelloapi.run.aws-usw02-pr.ice.predix.io/hello: statusCode: 200

Response Label:

{
text = "Hello from a Predix Service Protected by UAA";
}

Next Steps