iOS Trip detection#

Our SDK can detect vehicle type movements and notify your app when a relative trip is detected.

Warning: This feature consumes battery so if you are already moving, do not use it as a permanent move detection mechanism.

Get the SDK instance#

Like protection feature, the trip detection feature is available through 2 protocols, considering your programming habits or ways.

LRSafeRiderDelegationProtocol#

protocol LRSafeRiderDelegationProtocol: LRSafeRiderProtocol {

    var tripDetectionDelegate: LRTripDetectionDelegate?

    func enableTripDetection(for vehicleType: LRVehicleType)
    func muteTripDetection(_ isMuted: Bool)
    func disableTripDetection()
}

You can get this protocol as explained below

var safeRiderInstance = LRSafeRider.getShared(LRSafeRiderDelegationProtocol.self)!

The delegation protocol comes with one delegate that helps you to know what happens.

protocol LRTripDetectionDelegate {

    func onTripDetected(for vehicleType: LRVehicleType)
    func onTripDetectionError(_ error: LRSafeRiderError?)
}

The LRTripDetectionDelegate allows you to be notified of the detection of a trip for a given vehicle type or the possible SDK errors.

LRSafeRiderReactiveProtocol#

protocol LRSafeRiderReactiveProtocol: LRSafeRiderProtocol {

    var tripDetectionPublisher: AnyPublisher<LRVehicleType, Never> { get }
    var tripDetectionErrorPublisher: AnyPublisher<LRSafeRiderError?, Never> { get }

    func enableTripDetection(for vehicleType: LRVehicleType)
    func muteTripDetection(_ isMuted: Bool)
    func disableTripDetection()
}

You can get this protocol as explained below

var safeRiderInstance = LRSafeRider.getShared(LRSafeRiderReactiveProtocol.self)!

The reactive protocol provides basically the same services than the delegation one but exposing Combine publishers.

Enable the trip detection#

After having initialized the SDK, you can enable the trip detection feature for a given vehicle type. Before calling enableTripDetection(...), you must ensure that motion data could be accessed otherwise the SDK will return an error.

self.safeRiderInstance.enableTripDetection(for: .bicycle)

This will start a continuous service to detect a trip for the given vehicle type.

Mute the trip detection#

After having initialized the SDK and start the trip detection feature for a given vehicle type, you can mute temporarily it. You would not be notified of new trip detection for the given vehicle type until you won’t unmute it.

NB: It shall be good for saving device battery to mute the Trip detection feature as long as your app does not need to be notified when it has already received and processed a Trip detection event.

To mute the trip detection feature

self.safeRiderInstance.muteTripDetection(true)

To unmute the trip detection feature

self.safeRiderInstance.muteTripDetection(false)

Disable the trip detection#

After having initialized the SDK and start the trip detection for a given vehicle type, you can disable it.

self.safeRiderInstance.disableTripDetection()

Listen to the trip detection#

In order to be notified of a detected trip for a given vehicle type, you have to do, considering the relative SafeRider protocol you are using.

Using the LRSaferiderDelegationProtocol#

self.safeRiderInstance.tripDetectionDelegate = self

extension MyClass: LRTripDetectionDelegate {

    func onTripDetected(for vehicleType: LRVehicleType) {
        log.debug("trip detected for: \(vehicleType)")
    }

    func onTripDetectionError(_ error: LRSafeRiderError?) {
        log.error("trip detection error: \(error.localizedDescription)")
    }
}

Using the LRSaferiderReactiveProtocol#

var cancellableBag = Set<AnyCancellable>()

self.safeRiderInstance.tripDetectionPublisher
    .sink { vehicleType in
        log.debug("trip detected for: \(vehicleType)")
    }
    .store(in: &self.cancellableBag)

self.safeRiderInstance.tripDetectionErrorPublisher
    .sink { error in
        guard let error else { return }
            log.error("trip detection error: \(error.localizedDescription)")
        }
    .store(in: &self.cancellableBag)