Authenticate and Add the Live Experience Widget

Now that your Xcode project is prepared, you are ready to authenticate to Live Experience and add the widget to the UI of your iOS app.

  1. Click the ViewController.swift file from the tree view list of the project's files on the left pane of the Xcode window.
    The file displays in the center pane.
  2. Locate the line import UIKit and add import OracleLive below it.
  3. Locate the viewDidLoad() function and paste the following code below the line that says // Do any additional setup after loading the view, typically from a nib, and replace the following values with your own:
    • client_id: replace with your client ID

    • client_secret: replace with your client secret

    • tenantID: replace with your assigned tenant name

    • userID: replace with your user ID
      // Application credentials from LX console
      let client_id = "1fntfqg6k45nh4o8t3rt"
      let client_secret = "379709dd-c230-42d4-9f7a-7564501ccfb9"
      let loginString = String(format: "%@:%@", client_id, client_secret)
      let loginData = loginString.data(using: String.Encoding.utf8)!
      let base64LoginString = loginData.base64EncodedString()
      let url = URL(string: "https://live.oraclecloud.com/auth/apps/api/access-token?grant_type=client_credentials&nonce=&state=12345&scope=optional")!
      var request = URLRequest(url: url)
      // REST call requires Basic Auth
      request.httpMethod = "GET"
      request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
      let task = URLSession.shared.dataTask(with: request) { data, response, error in
       guard let data = data, error == nil else {
       print("XXXXXXXXX URLRequest error: \(error!.localizedDescription)")
       return
       }
       if let httpStatus = response as? HTTPURLResponse {
       // Check status code returned by the http server. Should be 200
       print("XXXXXXXXX HTTP status code: \(httpStatus.statusCode)")
       // Process result
       do {
       let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:Any]
       if let token = json?["access_token"] as? String {
       Controller.shared.service.authToken = token
       // LX Configuration
       Controller.shared.service.tenantID = "Your_Tenant_Name"
       Controller.shared.service.userID = "yourid@example.com"
       Controller.shared.contextAttributes.setValue("Collaboration", forKey: "appLocation")
       // Display the Widget
       Controller.shared.addComponent(viewController: self)
       } else {
       print("XXXXXXXXX Error: \(json?["description"] ?? "Unknown")")
       }
       } catch let error as NSError {
       print("XXXXXXXXX JSON parse errors: \(error)")
       }
       }
      }
      task.resume()

Results:

This code authenticates you with Live Experience using your tenant name (Your_Tenant_ID, which you replace with your own tenant name). After the authentication is successful, the Live Experience widget is added to your ViewController and displays to your customers using your app.