Integrate the Leeo iOS SDK
Before You Begin
We have created a sample app to demonstrate the basic use of the Leeo SDK. We have referred to this app in multiple places throughout the documentation. Here is the link for the app.
Requirements
- The SDK supports iOS 13 or above. Your app should target iOS 13 or above to use the SDK.
- You should have the latest stable version of Xcode installed.
- Sign in to the Leeo dashboard to access your Leeo SDK Key.
1. Setup your Project
You can set up your project in Xcode using CocoaPods (recommended) or using the Swift Package Manager
Installation using Cocoapods
The easiest way to integrate the Leeo iOS SDK and its dependencies into your application is by using the Leeo SDK CocoaPod.
-
Install CocoaPods. See the CocoaPods Guide.
-
Update CocoaPods.
$ sudo gem update cocoapods -
Create a Podfile and use it in Xcode. See Instructions.
-
Add the following to your
Podfileand run the command:pod update
pod 'FairmaticSDK', :git => 'https://github.com/fairmatic/fairmatic-cocoapods', :tag => '3.1.2'
If, after including the SDK, you get an error saying "duplicate symbols for architecture i386", then you might have already linked some frameworks directly. Importing the included frameworks with CocoaPods causes the duplication of the symbols.
In such a case, it is suggested that you remove the conflicting frameworks from your project and add them using CocoaPods.
Installation using Swift Package Manager
- Open your project in the latest Xcode.
- Go to File > Swift Packages > Add Package Dependency...
- In the field, enter package repository URL, enter https://github.com/fairmatic/fairmatic-sdk-spm
- Pick the latest version and click Next.
- Click Finish
- Add the
-ObjCflag to the Other Linker Flags section of your target's build settings.
2. Adjust Project Settings
- Allow background location updates for your app.
- On the project screen, click Capabilities.
- Turn Background Modes on.
- Select Location updates.
- Select Background Fetch
Location Permission Screen and Keys
The App Store publishes the following guidelines for apps using “Always Allow” location permission:
- Your app must have an explicit notification to users that location and driving behavior data are being tracked, and the purpose of this tracking.
- Your app must get explicit consent from users before collecting such information and disclose its use clearly. Directing users to and requiring acknowledgment of a privacy policy is not considered sufficient.
- Include the following keys in your app's
Info.plist:
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We need background location permission to provide you with
driving analytics</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>We need background location permission to provide you with
driving analytics</string>
<key>NSMotionUsageDescription</key>
<string>We use activity to detect your trips faster and more accurately.
This also reduces the amount of battery we use.</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Bluetooth</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Bluetooth</string>
Even though we won't actually use Bluetooth features, Apple requires this message whenever Bluetooth code is present in an app. This is just a technical requirement.
Background Task ID
For the Leeo SDK to be more accurate in uploading all trip data, it needs to have background fetch capability and a background task ID declared in your Info.plist file. You must add the following line in Info.plist file:
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.fairmatic.sdk.bgrefreshtask</string>
</array>
In the case you already have a background refresh task, as iOS allows only one scheduled background fetch task, you will need to reuse your existing BGAppRefreshTask task to call the following function:
Leeo.logSDKHealth(.backgroundProcessing) { _ in
// task.setTaskCompleted(success: success)
}
In this case, don’t add the new BGTaskSchedulerPermittedIdentifiers to your Info.plist.
3. Set up the Leeo SDK in code
The following code snippet shows how to initialize the SDK. You will need the SDK key and the driver details.
- Swift
- Objective-C
// 1. Import the Leeo SDK module
import FairmaticSDK
// 2. Driver attributes creation
let driverAttributes = DriverAttributes(firstName: "John",
lastName: "Doe",
email: "john_doe@company.com",
phoneNumber: "1234567890")
// 3. Creation of the config object using the SDK key,
// driverId, and the above driver attributes.
let config = Configuration(sdkKey: "your_sdk_key_here",
driverId: "alphanumeric_driver_id",
driverAttributes: driverAttributes)
// 4. Set up the SDK with the config object, and check for success.
Fairmatic.setupWith(
configuration: config
) { [weak self] success, error in
if success {
print("Leeo SDK setup successfully!")
} else {
print("Failed to init the Leeo SDK, error: \(error)")
}
}
// 1. Import the Leeo SDK
@import FairmaticSDK;
// 2. Driver attributes creation
FairmaticDriverAttributes *driverAttributes =
[[FairmaticDriverAttributes alloc] initWithFirstName:@"John"
lastName:@"Doe"
email:@"john_doe@company.com"
phoneNumber:@"1234567890"];
// 3. Creation of the config object using the above objects,
// driverId and SDK key
FairmaticConfiguration *config =
[[FairmaticConfiguration alloc] initWithSdkKey:@"your_sdk_key_here"
driverId:@"alphanumeric_driver_id"
driverAttributes:driverAttributes];
// 4. Set up the Leeo SDK with the configuration object.
[Fairmatic setupWithConfiguration:config completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"Leeo SDK setup successful.");
} else {
NSLog(@"Leeo SDK setup failed with error: %@", error.localizedDescription);
}
}];
Please put this code in your AppDelegate's didFinishLaunchingWithOptions() method if the driver details are already available in your app. This code should also be present in the flow when your driver logs in successfully to the app. The SDK's Fairmatic.setup() API should be called at every app launch with proper configuration. Failing to do so will result in errors in the trip APIs.
We recommend that in the completion handler of the setup API, you check the error parameter to be nil and the success parameter to be true if the SDK is set up or not. This is important to ensure that your application flow is not broken and the SDK APIs can be called properly.
Handling setup() API Errors
The implementation included in this document for the setup() API function handles the errors returned in the completion handler. You can use that completion handler to display or hide error notifications for the setup() API. See our FairmaticError Appledoc for more details.