To make requests to a vehicle, your end user must connect their vehicle using Smartcar Connect. Smartcar Connect uses the OAuth 2.0 protocol to provide a secure and elegant authorization flow for your users.

1. Initialize the Smartcar object

Instantiate a Smartcar object in the viewdidLoad function of the ViewController.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
// getting-started-ios-sdk/ViewController.swift

// TODO: Authorization Step 1: Initialize the Smartcar object
appDelegate.smartcar = SmartcarAuth(
    clientId: Constants.clientId,
    redirectUri: "sc\(Constants.clientId)://exchange",
    testMode: true,
    scope: ["required:read_vehicle_info"],
    completion: completion
)

2. Launch Connect

The iOS application will launch a SafariView with Smartcar Connect to request access to a user’s vehicle. On Connect, the user logs in with the username and password for their vehicle’s connected services account and grants the application access to their vehicle.

To launch Connect, we can use the launchAuthFlow function that our Smartcar object has access to. We can place this within the connectPressed action function.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
// getting-started-ios-sdk/ViewController.swift

@IBAction func connectPressed(_ sender: UIButton) {
    // TODO: Authorization Step 2: Launch Connect
    let smartcar = appDelegate.smartcar!
    smartcar.launchAuthFlow(viewController: self)
}

3. Receive an authorization code

Once a user has authorized the application to access their vehicle, the user is redirected to the redirect_uri with an authorization code as a query parameter.

iOS applications use custom URI schemes to intercept calls and launch the relevant application. This is defined within the Info.plist. Open up Info.plist and click on the grey arrow next to “URL types”. Next, click on the grey arrow next to the “Item 0” dictionary and “URL Schemes” array. Finally, set your “Item 0” string to the redirect URI (i.e. 'sc' + clientId).

The iOS application will now receive the request in the application:(_:open:options:) function within the AppDelegate.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
// getting-started-ios-sdk/AppDelegate.swift
 
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    // TODO: Authorization Step 3a: Receive the authorization code
    window!.rootViewController?.presentedViewController?.dismiss(animated: true , completion: nil)
    smartcar!.handleCallback(with: url)
    
    return true
}

Using the iOS SDK, the application can receive the code in the completion callback passed into the Smartcar object.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
// getting-started-ios-sdk/ViewController.swift

func completion(err: Error?, code: String?, state: String?) -> Any {
    // TODO: Authorization Step 3b: Receive the authorization code
    print(code!);
    //prints out the authorization code
}

Try it out

Let’s try authenticating a vehicle in test mode.

In test mode, any username or password is valid for each brand.

Build your application in XCode and click on the “Connect your vehicle” button. Notice once you log in, Smartcar showcases all the permissions your application is asking for, in this case, read_vehicle_info. A user has to consent to all the permissions.

Once you have logged in and accepted the permissions, you should see your authorization code printed to your console.


In the next section, we will cover how to exchange the authorization code for an access_token and make your first request to Smartcar API with it!