iOS Motion data policy#

Your app must have access to the Motion data if you want to use the Trip detection feature. This access is driven by the Motion & Fitness authorization.

Access to Motion data (iOS level)#

First of all, your app must ensure, at any time, to have access to Motion data.

See Apple’s CMMotionActivityManager description.

Include Motion Usage Description key#

To use this API, you must include the NSMotionUsageDescription key in your app’s Info.plist file and provide a usage description string for this key.

The usage description appears in the prompt that the user must accept the first time the system asks the user to access motion data for your app.

If you don’t include a usage description string, your app crashes when our SDK will call this API.

Request Motion & Fitness authorization#

Before requesting relative authorization, you have to check if you do not already have it.

CMMotionActivityManager.authorizationStatus() == .authorized

If the autorization status is different from .authorized, you have to request the autorization to the user. While he won’t grant it, you could not use the Trip detection feature.

let now = Date()
let manager = CMMotionActivityManager()
manager.queryActivityStarting(from: now, to: now, to: .main) { motionActivities, error in
    if let nsError = (error as? NSError), nsError.code == Int(CMErrorMotionActivityNotAuthorized.rawValue) {
        // redirect to app settings screen
    }
}
manager.stopActivityUpdates()

NB: When the user will grant you this autorization not at the first request, your app will be relaunched when you come back on it. If you do not want this behavior, see Restoring your App’s State for SwiftUI or UIKit.