iOS

Request

Now that the iOS application has an authorization code, it can exchange it for an access_token required to make a request to a vehicle.


Step 1: Obtain An Access Token

After receiving the authorization code, the iOS application must exchange it for an access_token. To do so, we can send the code to the back-end service which will implement in a bit. Until then, let's assume our back-end service contains an endpoint /exchange that receives an authorization code as a query parameter and exchanges it for an access_token.

We can add this logic in our completion callback -

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
// getting-started-ios-sdk/ViewController.swift

// TODO: Request Step 1: Obtain an access token
Alamofire.request("\(Constants.appServer)/exchange?code=\(code!)", method: .get)
    .responseJSON {_ in}

Notice that our back-end service does not return the access_token. This is by design. For security, our front-end should never have access to the access_token and should always be stored in the back-end.

Step 2: Get Vehicle Information

Once the backend has the access_token, it can send requests to a vehicle using the Smartcar API. The iOS app will have to send a request to the back-end service which in turn sends a request to Smartcar. We have to do this because our front-end does not have the access_token.

Assuming our back-end has a /vehicle endpoint that returns the information of a user's vehicle, we can make this query in our completion callback and segue into another view the returned vehicle info -

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
// getting-started-ios-sdk/ViewController.swift

func completion(err: Error?, code: String?, state: String?) -> Any {

    
    // send request to exchange auth code for access token
    Alamofire.request("\(Constants.appServer)/exchange?code=\(code!)", method: .get).responseJSON {_ in

        // TODO: Request Step 2: Get vehicle information
        // send request to retrieve the vehicle info
        Alamofire.request("\(Constants.appServer)/vehicle", method: .get).responseJSON { response in
            print(response.result.value!)
            
            if let result = response.result.value {
                let JSON = result as! NSDictionary
                
                let make = JSON.object(forKey: "make")!  as! String
                let model = JSON.object(forKey: "model")!  as! String
                let year = String(JSON.object(forKey: "year")!  as! Int)
                
                let vehicle = "\(year) \(make) \(model)"
                self.vehicleText = vehicle
                
                self.performSegue(withIdentifier: "displayVehicleInfo", sender: self)
            }
        }
    }
    
    return ""
}

Step 3: Back-End Service

Now that our front-end is complete, we will need to create a back-end service that contains the logic for the /exchange and /vehicle endpoints.

You can use any of our back-end SDKs to set up the service. For set up instructions, you can read the Setup section from our Getting Started guide. Once you are set up, you can follow along with the Making a Request section.

Note: you can ignore the Try It Out section of the Getting Started guide, and instead follow along with the next section.

Try It Out

Make sure your back-end server is up and running. In your back-end directory -

  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
  • $
export REDIRECT_URI=sc + "clientId" + ://exchange
node index.js

Now run the iOS application in XCode!

Next Steps