This snippet assumes that the required configurations and permissions are already setup to work. Check the permissions handling section for more details.
SDK Authorization and Setup
Users will need to be authenticated. This authentication will persist until shutdown is called. It will persist over the termination of the app. The configuration to enable functionality is passed here. startAutomaticDetection will start the services. This too will persist and services will automatically start when launching the app from the background.
The authentication object is constructed with tokens that need to be acquired from the backend (either from the SDK backend directly, or via your product backend). See MOVE Backend.
importDolphinMoveSDK// Response from the API Request.// {// "authCode": "string"// }let authCode =...let sdkConfig =MoveConfig( detectionService: [ .driving([.drivingBehavior, .distractionFreeDriving, .deviceDiscovery]), .walking([.location]), .cycling, .places, .publicTransport, .pointsOfInterest, .assistanceCall, .automaticImpactDetection])MoveSDK.shared.setup(authCode: authCode, config: config) { status in...}...MoveSDK.shared.startAutomaticDetection()...
Authentication Invalidation
The host app is expected to monitor MoveAuthState updates viaauthStateChangeListener(iOS) API and handle those changes accordingly.
An auth state listener must be implemented to handle user invalidation, which can happen due to a user being blocked or having multiple conflicting installations with the same user.
The host app is expected to set its SDKStateListenerbefore initializing the SDK to intercept the MoveSDKState changes.
The provided block could then start the SDK when MoveSDKState is ready or handle errors if occurred. The provided block could look something like this:
MoveSDK.shared.setSDKStateListener { state inswitch state {case .uninitialized:/* SDK uninitialized*/breakcase .ready:/* SDK initialised and ready to start the service*/breakcase .running:/* SDK Started Detection*/break }}
SDK Initialization
TheinitializationAPI must be executed beforedidFinishLaunchingWithOptionsreturns. We recommend calling it inwillFinishLaunchingWithOptions.
Exceptions might apply, where the SDK is not initialized on app launch. First initialization is a good example, where the app would only initialize the SDK after onboarding the user and requesting permissions.
Check Initialization Timing for more details about the importance of adequately initializing the SDK.
When services are not running properly because they are missing permissions or are not authorized on the backend, warnings or errors will be reported in the corresponding listeners.
MoveSDK.shared.setServiceFailureListener { failures infor failure in failures {/* a list of MoveServiceFailure objects */switch failure.reason {case .unauthorized:/* a service is not configured on the backend */breakcaselet .missingPermission(permissions):/* a required permission is missing */break } }}MoveSDK.shared.setServiceWarningListener { warnings infor warning in warnings {/* a list of MoveServiceWarning objects */switch warning.reason {caselet .missingPermission(permissions):/* an optional permission is not provided, data collection will be reduced */break } }}
WORKING SAMPLE CODE
importUIKitimportCoreMotionimportCoreLocationimportDolphinMoveSDK@mainclassAppDelegate:UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {var locationManager =CLLocationManager()let activityManager =CMMotionActivityManager() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// ACQUIRE PERMISSIONSsetupPermissions()// INITIALIZE MOVE SDKinitializeSDK()returntrue }/// INITIALIZE MOVE SDKfuncinitializeSDK(launchOptions: [UIApplication.LaunchOptionsKey:Any]?) {setupSDKListeners() MoveSDK.shared.initialize(launchOptions: launchOptions)if MoveSDK.shared.getSDKState()== .uninitialised { self.setupUser() } }/// SETUP LISTENERSfuncsetupSDKListeners() { MoveSDK.shared.setSDKStateListener { state inswitch state {case .uninitialized:print("SDK uninitialized")breakcase .ready:print("SDK initialised and ready to start the service")breakcase .running:print("SDK Started Detection")break } } MoveSDK.shared.setServiceFailureListener { failures infor failure in failures {/* a list of MoveServiceFailure objects */switch failure.reason {case .unauthorized:/* a service is not configured on the backend */print("unauthorized")caselet .missingPermission(permission):/* a required permission is missing */print(permission)break } } } MoveSDK.shared.setServiceWarningListener { warnings infor warning in warnings {/* a list of MoveServiceWarning objects */switch warning.reason {caselet .missingPermission(permissions):/* an optional permission is not provided, data collection will be reduced */print(permissions)break } } } }/// ACQUIRE PERMISSIONSfuncsetupPermissions() { locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation()switch CMMotionActivityManager.authorizationStatus() {case .restricted, .denied:print("UNAUTHORIZED")case .authorized:print("AUTHORIZED")case .notDetermined:let activityManager =CMMotionActivityManager() activityManager.queryActivityStarting(from: Date(), to: Date(), to: .main) { (activity, error) in } } }/// SETUP USERfuncsetupUser() {// API REQUEST - Post Request for Auth Code: // Example: https://stackoverflow.com/a/26365148let authCode =...// SDK CONFIGURATION WITH SERVICESlet sdkConfig =MoveConfig( detectionService: [ .driving([.drivingBehavior, .distractionFreeDriving]), .walking([]), .cycling, .places, .publicTransport, .pointsOfInterest, .assistanceCall, .automaticImpactDetection ])// SETUP THE SDK WITH AUTH AND SDK CONFIG OBJECTS MoveSDK.shared.setup(authCode: authCode, config: sdkConfig) { status in }// START THE SDK SERVICES MoveSDK.shared.startAutomaticDetection() }}