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¶
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¶
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.
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)