# Advanced configurations

## Manually initialize the SDK

By default, the DriveKit SDK is automatically initialized as soon as you add the TripAnalysis module to your project.&#x20;

In some cases, you may want to manually initialize the SDK. To do this, you have to disable the automatic initialization by adding the key `DKAutoInitEnabled` associated to the Boolean value `false` into the `Info.plist` file:

{% tabs %}
{% tab title="Info.plist" %}

```xml
<key>DKAutoInitEnabled</key>
<false/>
```

{% endtab %}
{% endtabs %}

Then, you must call the initialization method in `didFinishLaunchingWithOptions` method of your `AppDelegate`.

{% tabs %}
{% tab title="AppDelegate" %}

```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    DriveKit.shared.initialize()
    DriveKitTripAnalysis.shared.initialize(appLaunchOptions: launchOptions)
    (…)
}
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
The DriveKit modules include a method `initialize` that must be called in `didFinishLaunchingWithOptions` method of your `AppDelegate` if you have decided to manually initialize the SDK.
{% endhint %}

## Check your configuration

You can check if DriveKit is well configured with the following method:

{% tabs %}
{% tab title="DriveKit" %}

```swift
func isConfigured() -> Bool
```

{% endtab %}
{% endtabs %}

This method returns `true` if these three conditions are met:

* DriveKit Core is initialized
* an API key is set
* a userId is set

## Check if user is authenticated

You can check if the user is connected by calling the following method:

{% tabs %}
{% tab title="DriveKit" %}

```swift
func isUserConnected() -> Bool
```

{% endtab %}
{% endtabs %}

This method returns `true` if the user is authenticated to DriveKit.

## Logging

DriveKit comes with a logging feature that is enabled by default. This feature allows you to quickly identify the cause of a problem. We recommend leaving the log enabled as it does not consume memory space and is useful in the support phase. However, if you don't want to use it, it can be disabled.

{% hint style="info" %}
Log will be written in app directory. One log file per month will be written with the name `log-<YEAR>-<MONTH>.txt` (example: `log-2019-8.txt`). All DriveKit modules log in this file.\
You can get a zip file with the log files of the previous month and the current one with the method `DriveKitLog.shared.getZippedLogFilesUrl()`, or by clicking on “Contact support” and changing the email receiver. The file will be in attachment of the email.

You can also make files of your application (including DriveKit log files) available in the [iOS Files app](https://support.apple.com/en-us/HT206481) by adding these 2 keys to your project's `Info.plist` file: `UIFileSharingEnabled` and `LSSupportsOpeningDocumentsInPlace,` setting them both to `true.`
{% endhint %}

{% hint style="info" %}
To be able to access logs from `Files` app, you must add the following entries in your `Info.plist` file: `UIFileSharingEnabled` and `LSSupportsOpeningDocumentsInPlace`, setting both to `true`.
{% endhint %}

Disable logging by calling the following method:

{% tabs %}
{% tab title="DriveKit" %}

```swift
func disableLogging(showInConsole: Bool = true)
```

{% endtab %}
{% endtabs %}

To activate logging, call the following method:

{% tabs %}
{% tab title="DriveKit" %}

```swift
func enableLogging(showInConsole: Bool = true)
```

{% endtab %}
{% endtabs %}

The DriveKit log ZIP file includes a configuration file in JSON format.&#x20;

This file lists the values of all configuration parameters for the DriveKit SDK installed in your user's app.

This [document](https://docs.google.com/spreadsheets/d/1cdU7jM3gLEyFBWpaHf5OH9SSRZkm38YACrohn0Cr-t4/edit?usp=sharing) provides a detailed overview of all the configuration entries included in the config file, along with their definitions and expected values.<br>

## Listen for permissions and sensors status changes

If the user disables the sensors or revokes permissions, the application would not be able to detect and record a trip. To avoid this, the SDK identifies important sensor state and permission changes.&#x20;

These events are shared and you can use them in your application to inform the user via a visual alert or by displaying a notification.

{% hint style="info" %}
Event changes callbacks may not be fired in real time due to technical restrictions of iOS.
{% endhint %}

### DKDeviceConfigurationDelegate

`DKDeviceConfigurationDelegate` is a protocol used to get callbacks when device configuration changes are detected:

```swift
protocol DKDeviceConfigurationDelegate: AnyObject {
    func deviceConfigurationDidChange(event: DKDeviceConfigurationEvent)
}
```

### Add a delegate

To add a delegate and listen for [Device Configuration Events](#dkdeviceconfigurationevent), you can call the following function:

{% tabs %}
{% tab title="DriveKit" %}

```swift
func addDeviceConfigurationDelegate(_ delegate: DKDeviceConfigurationDelegate)
```

{% endtab %}
{% endtabs %}

### Remove a delegate

&#x20;To remove a specific delegate, call the following method:

{% tabs %}
{% tab title="DriveKit" %}

```swift
func removeDeviceConfigurationDelegate(_ delegate: DKDeviceConfigurationDelegate)
```

{% endtab %}
{% endtabs %}

### Remove all delegates

&#x20;To remove all delegates, call the following method:

{% tabs %}
{% tab title="DriveKit" %}

```swift
func removeAllDeviceConfigurationDelegates()
```

{% endtab %}
{% endtabs %}

### DKDeviceConfigurationEvent

`DKDeviceConfigurationEvent` is a class describing a device configuration event.

```swift
class DKDeviceConfigurationEvent {
    let type: DKDeviceConfigurationEventType
    let isValid: Bool
}
```

<table><thead><tr><th width="153">Attribute</th><th width="95.33333333333331">Type</th><th>Description</th></tr></thead><tbody><tr><td>type</td><td>Enum</td><td><a href="#dkdeviceconfigurationeventtype"><code>DKDeviceConfigurationEventType</code></a> enum describing the type of event</td></tr><tr><td>isValid</td><td>Boolean</td><td>Boolean describing whether the device configuration event is valid or not</td></tr></tbody></table>

### DKDeviceConfigurationEventType

```swift
public enum DKDeviceConfigurationEventType {
    case activityPermission
    case locationPermission
    case bluetoothPermission
    case notificationPermission
    case lowPowerMode
    case locationSensor
    case bluetoothSensor
}
```

Possible types are:

<table><thead><tr><th width="284">Event type</th><th>Description</th></tr></thead><tbody><tr><td><code>activityPermission</code></td><td>Motion &#x26; Fitness permission status changed</td></tr><tr><td><code>locationPermission</code></td><td>Location permission status changed</td></tr><tr><td><code>bluetoothPermission</code></td><td>Bluetooth permission status changed</td></tr><tr><td><code>notificationPermission</code></td><td>Notifications permission status changed</td></tr><tr><td><code>lowPowerMode</code></td><td>Low Power Mode status changed</td></tr><tr><td><code>locationSensor</code></td><td>Location sensor status changed</td></tr><tr><td><code>bluetoothSensor</code></td><td>Bluetooth sensor status changed</td></tr></tbody></table>

The DriveKit SDK will run optimally if the isValid value of each event is true. The table below explains the impact of a status that is not true.

<table><thead><tr><th width="288.3333333333333">Event</th><th width="106" align="center">Criticality</th><th>Consequence if value is false</th></tr></thead><tbody><tr><td><code>LocationPermission</code></td><td align="center">🔴</td><td>No trip recording if app has no access to GPS sensor</td></tr><tr><td><code>BluetoothPermission</code></td><td align="center">🟡</td><td>No trip detection based on Bluetooth system</td></tr><tr><td><code>ActivityPermission</code></td><td align="center">🟡</td><td>Detected transportation mode mode can be inaccurate when no access to this permission</td></tr><tr><td><code>NotificationPermission</code></td><td align="center">🟢</td><td>Your application cannot send notification to the user if the permission is revoked.</td></tr><tr><td><code>lowPowerMode</code></td><td align="center">🟢</td><td>No considerable impact</td></tr><tr><td><code>LocationSensor</code></td><td align="center">🔴</td><td>No trip recording if GPS sensor is disabled</td></tr><tr><td><code>BluetoothSensor</code></td><td align="center">🟡</td><td>No trip detection based on Bluetooth system.<br>Can also impact beacon trip detection.</td></tr></tbody></table>

## Get user’s information

To get user’s information (first name, last name and pseudo), call the `getUserInfo` method. It will retrieve and save these data locally:

```swift
DriveKit.shared.getUserInfo(synchronizationType: .defaultSync) { status, userInfo in
    if status == .success {
        // Get user's names in userInfo object.
    }
}
```

## Update user’s information

You can add information to a user's account such as first name, last name and pseudo. These details are optional and you can choose to make the user's account anonymous. To update the user's information, you must call the `updateUserInfo` method:

```swift
DriveKit.shared.updateUserInfo(pseudo: "New_pseudo") { success in
    if success {
        // The pseudo has been successfully updated.
    }
}
```

Or to update all information:

```swift
DriveKit.shared.updateUserInfo(firstname: "New_firstname", lastname: "New_lastname", pseudo: "New_pseudo") { success in
    if success {
        // The firstname, lastname and pseudo have been successfully updated.
    }
}
```

## Update UserId

It is possible to update the userId by calling the following method:

{% tabs %}
{% tab title="DriveKit" %}

```swift
func updateUserId(userId: String)
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
To be able to check whenever `userId` got updated and catch the update status, you have to use [`DriveKitDelegate`](https://docs.drivequant.com/get-started-drivekit/references#drivekitdelegate) delegate.
{% endhint %}

## Account deletion

You can delete a driver's account in DriveKit. This action deletes all the data related to the account.

&#x20;The deletion can be done instantly or with delay.&#x20;

* In the first case, when the method is called, the account is instantly deleted.&#x20;
* In the second case, the driver has 30 days to log back into the application and reactivate his account.

To delete a driver's account, use the following method:

{% tabs %}
{% tab title="DriveKit" %}

```swift
func deleteAccount(instantDeletion: Bool = false)
```

{% endtab %}
{% endtabs %}

`instantDeletion` can have 2 values:&#x20;

* `false` : Default value, allows the user to recover the deleted account by logging-in again with the same credentials. Users have 30 days starting from the day when the account was deleted.
* `true` : Allow to delete an account instantly. The account and all the related data will be immediately deleted and no rollback is possible.

{% hint style="info" %}
Your team needs to have the deletion feature activated to use this method. Please contact DriveQuant if you need it.
{% endhint %}

{% hint style="info" %}
To be able to check whenever the account deletion is complete, you have to use the [DriveKitDelegate](https://docs.drivequant.com/get-started-drivekit/ios/references#drivekitdelegate) interface.
{% endhint %}

{% hint style="warning" %}
You should restore the DriveKit API key in the `onAccountDeleted()` callback only when the status value is `SUCCESS`.
{% endhint %}

## Retrieving the installation identifier

The installation identifier (**installationId**) is not a unique device identifier. It is used to identify an app installation that includes the DriveKit SDK, linked to a specific account on a particular device.

The **installationId** is generated based on the following attributes:&#x20;

* Installation date
* App
* User account
* Device type

The **installationId** helps determine whether a user has logged into a mobile app with the same account across multiple devices.

For a user with one account and a single device, the **installationId** behaves as follows:

* It remains unchanged if the user logs out and logs back in, as long as the app is not uninstalled.
* It is updated when the user uninstalls and reinstalls the app.

\
If a user logs into the app on several devices using the same account, each device will have a different **installationId**.

If the user reconnects to the app on the same device but with a different account, the installationId will be updated.

{% hint style="info" %}
The installationId uses the UUID format. Example: `123e4567-e89b-12d3-a456-426614174000`
{% endhint %}

You can retrieve the **installationId** by calling the following computed property:

{% tabs %}
{% tab title="DriveKit" %}

```swift
var installationId: String?
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
The returned installationId will be `nil` as long as the user is not authenticated to DriveKit.

For example, you can retrieve the identifier after the `driveKitDidConnect()` callback is triggered. (Read more about the [DriveKitDelegate](https://docs.drivequant.com/get-started-drivekit/references#drivekitdelegate))
{% endhint %}

## Reset

If you need to reset DriveKit configuration (user logout for example), you can call the following method:

```swift
DriveKit.shared.reset()
```

All data saved locally will be erased and default configuration for every module will be restored.
