# TripListener

The `TripListener` protocol provides useful information and events about trips analyzed by DriveKit.

For example, you can be informed when a trip analysis has started, finished, canceled, when a crash is detected, etc.

{% hint style="info" %}
The trip recording lifecycle is exhaustively described in [Trip recording lifecycle](https://docs.drivequant.com/get-started-drivekit/trip-recording-lifecycle) part.
{% endhint %}

## Add a TripListener

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

```swift
func addTripListener(_ tripListener: TripListener)
```

{% endtab %}
{% endtabs %}

You can remove a specific listener using the following method:

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

```swift
func removeTripListener(_ tripListener: TripListener)
```

{% endtab %}
{% endtabs %}

To remove all TripListeners objects:

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

```swift
func removeAllTripListeners()
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
If you remove all your trip listeners or forget to add one, your app will not receive any feedback from the trip analysis module. It will not prevent the analysis from working but your code will not be able to execute its own logic (UI updates, notification, etc.).
{% endhint %}

{% hint style="warning" %}
Do not forget to remove your `TripListener` objects when you don't need it anymore.
{% endhint %}

`TripListener` interface includes several methods to implement:

| Method                                                                                                                                                                                                                                                                                   | Description                                                                                                                                                                                                                                                                                                                                                                                                        |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| tripRecordingStarted(state: DKTripRecordingStartedState)                                                                                                                                                                                                                                 | <p>Immediately called when a trip recording starts.</p><p>This callback is triggered:</p><ul><li>after calling the DriveKit SDK's <a href="../tripmanagement#manually-start-a-trip"><code>startTrip()</code></a> method.</li><li>after automatic trip detection.</li></ul><p><code>DKTripRecordingStartedState</code> object is described <a href="../references#dktriprecordingstartedstate">here</a>.</p>        |
| tripRecordingConfirmed(state: DKTripRecordingConfirmedState)                                                                                                                                                                                                                             | <p>Called each time a trip is confirmed.<br><code>DKTripRecordingConfirmedState</code> object is described <a href="../references#dktriprecordingconfirmedstate">here</a>.</p>                                                                                                                                                                                                                                     |
| tripRecordingCanceled(state: DKTripRecordingCanceledState)                                                                                                                                                                                                                               | <p>Called when a trip recording is canceled. <code>DKTripRecordingCanceledState</code> indicates which event has canceled the trip.</p><p><code>DKTripRecordingCanceledState</code> object is described <a href="../references#dktriprecordingcanceledstate">here</a>.</p>                                                                                                                                         |
| tripRecordingFinished(state: DKTripRecordingFinishedState)                                                                                                                                                                                                                               | <p>Called when trip recording has ended, before sending trip data to DriveQuant's servers.<br><code>DKTripRecordingFinishedState</code> object is described <a href="../references#dktriprecordingfinishedstate">here</a>.</p>                                                                                                                                                                                     |
| tripFinished(responseStatus: TripResponseStatus)                                                                                                                                                                                                                                         | <p>This method is called when a trip has been recorded by the SDK and analyzed by the DriveQuant's servers.</p><p></p><p><a href="../references#tripresponsestatus"><code>TripResponseStatus</code></a> object contains the trip analysis made on DriveQuant's server.</p>                                                                                                                                         |
| tripPoint(tripPoint: TripPoint)                                                                                                                                                                                                                                                          | Called when a trip is started and confirmed, for each GPS point recorded by the SDK. Data available in `TripPoint` object are described [here](https://docs.drivequant.com/trip-analysis/references#trippoint).                                                                                                                                                                                                    |
| tripSavedForRepost()                                                                                                                                                                                                                                                                     | Called if at the end of the trip, the trip couldn't be sent to DriveQuant's server for the analysis, for example when the smartphone has no network. The trip is saved locally on the SDK and will automatically be sent later.                                                                                                                                                                                    |
| beaconDetected()                                                                                                                                                                                                                                                                         | <p>Called when a beacon sets in the SDK is detected.<br>It is triggered exclusively if the trip recording has not been started yet (i.e. during the <code>INACTIVE</code> <a href="../tripmanagement#sdk-recorder-state">state</a>)</p>                                                                                                                                                                            |
| beaconConfirmed(beacon: BeaconData)                                                                                                                                                                                                                                                      | <p>Called when a beacon set in the SDK is detected and confirmed during a trip.<br>It is triggered exclusively if the trip recording has started (i.e. during the <code>RUNNING</code> <a href="../tripmanagement#sdk-recorder-state">state</a>)</p>                                                                                                                                                               |
| significantLocationChangeDetected()                                                                                                                                                                                                                                                      | Called when a user significant location change is detected.                                                                                                                                                                                                                                                                                                                                                        |
| sdkStateChanged(state: State)                                                                                                                                                                                                                                                            | Called every time the state of the SDK changed with the new state as parameter. States are described [here](https://docs.drivequant.com/trip-analysis/tripmanagement#sdk-recorder-state).                                                                                                                                                                                                                          |
| crashDetected(crashInfo: DKCrashInfo)                                                                                                                                                                                                                                                    | Called when a crash event is detected. Triggered if Crash Detection is enabled. Read more [here](https://docs.drivequant.com/trip-analysis/ios/crash-detection#enable-crash-detection)                                                                                                                                                                                                                             |
| crashFeedbackSent(crashInfo: DKCrashInfo, feedbackType: DKCrashFeedbackType, severity: DKCrashFeedbackSeverity)                                                                                                                                                                          | called when crash feedback is enabled and a confirmed crash is detected. Triggered if Crash Detection is enabled. Read more [here](https://docs.drivequant.com/trip-analysis/ios/crash-detection#enable-crash-detection)                                                                                                                                                                                           |
| <p><del>tripStarted(startMode: StartMode)</del><br><span data-gb-custom-inline data-tag="emoji" data-code="26a0">⚠️</span> <em>This method has been deprecated to be replaced by:</em> <code>tripRecordingConfirmed(state: DKTripRecordingConfirmedState)</code></p>                     | Called each time a trip is started. `StartMode` indicates which event starts the trip. Possible values are described [here](https://docs.drivequant.com/trip-analysis/references#startmode).                                                                                                                                                                                                                       |
| <p><del>tripCancelled(cancelTrip: CancelTrip)</del><br><span data-gb-custom-inline data-tag="emoji" data-code="26a0">⚠️</span> <em>This method has been deprecated to be replaced by:</em> <code>tripRecordingCanceled(state: DKTripRecordingCanceledState)</code></p>                   | Called when a trip is cancelled. `CancelTrip` indicates which event cancels the trip. Possible values are described [here](https://docs.drivequant.com/trip-analysis/references#canceltrip).                                                                                                                                                                                                                       |
| <p><del>tripFinished(post: PostGeneric, response: PostGenericResponse)</del></p><p><span data-gb-custom-inline data-tag="emoji" data-code="26a0">⚠️</span> <em>This method has been deprecated to be replaced by:</em> <code>tripFinished(responseStatus: TripResponseStatus)</code></p> | <p>This method is called when a trip has been recorded by the SDK and analyzed by the DriveQuant's servers.</p><p></p><p><code>PostGeneric</code> object contains raw data sent to DriveQuant's server, <code>PostGenericResponse</code> object contains the trip analysis made on DriveQuant's server. Detailed description of these data are available <a href="../../rest-services/trip#response">here</a>.</p> |

{% hint style="warning" %}
**End of trip notification**: Depending on the trip analysis service's response in the `tripFinished()` callback, we recommend that you specify the notification content if you decide to display a user feedback:&#x20;

* For a vehicle trip, you can display a message such as: `Your trip has been analyzed`.&#x20;
* For a trip by rail it is better to display the following message: `Your trip was made by train.`
  {% endhint %}

## Get the trip response status (Deprecated)

{% hint style="info" %}
This method has been deprecated and was [removed in DriveKit 3.0.0](https://docs.drivequant.com/get-started-drivekit/ios/drivekit-3.0-migration-guide#method-replacements).

The callback method `tripFinished(post: PostGeneric, response: PostGenericResponse)` is replaced by `tripFinished(responseStatus: TripResponseStatus)` which directly provides the useful information (see [TripResponseStatus](https://docs.drivequant.com/trip-analysis/references#tripresponsestatus)).
{% endhint %}

Once the DriveQuant servers has analyzed a trip, the `tripFinished()`  callback is triggered with the data in the `PostGenericResponse` object.

It can be useful to check the trip response status in order to check for example if the trip is valid or not with detailed information.

To do this, call the following method:

```swift
func getTripResponseStatus(_ tripResponse: PostGenericResponse) -> TripResponseStatus
```

The [TripResponseStatus](https://docs.drivequant.com/trip-analysis/references#tripresponsestatus) model is described in the References part.
