iOS

Authorization

To make requests to a vehicle, your end user must connect their vehicle using Smartcar's authorization flow. This flow follows the OAuth2 authorization code grant to gain access to resources on Smartcar.


Step 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
  • 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: Authorization Step 1: Initialize the Smartcar object
appDelegate.smartcar = SmartcarAuth(
    clientId: Constants.clientId,
    redirectUri: "sc\(Constants.clientId)://exchange",
    development: true,
    completion: completion
)

Step 2: Launch The Authorization Flow

The iOS application will launch a SafariView with the Smartcar Authorization Dialog to request access to a user's vehicle. On the Authorization Dialog, the user logs in with their vehicle credentials and grants the application access to their vehicle.

To launch the Authorization Dialog, 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
  • 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

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

Step 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
  • 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/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
  • 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 {
    // 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 login credentials for all brands are valid!

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!