Create an iPhone App

Requirements

Once you created an iPhone App you need to add a few entries to the .plist file:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>...</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>...</string>

<key>NSLocationUsageDescription</key>
<string>...</string>

<key>NSLocationWhenInUseUsageDescription</key>
<string>...</string>

<key>NSMotionUsageDescription</key>
<string>...</string>

Further you have to give your App the permission to collect location data in the background. So please add the background capability to your app and activate the mode location updates.

Installing the MotionSDK

Download the SDK and unzip it. Please contact Dev-Ops team for the password. MotionSDK.dmg

Select the General tab and drop the MotionSDK.xcframework under Frameworks, Libraries and Embedded Content

Using the MotionSDK

All you need to do is to create an instance of the AutomaticRecorder class:

@ObservedObject var tripRecorder = AutomaticRecorder()

There are two required imports:

import CoreLocation
import MotionSDK

A view showing the complete recorder state can be achieved like this:

var body: some View {
  VStack {
    Group {
      Text("Recorder: \(tripRecorder.state.rawValue)")
      Text("Authorization: \(tripRecorder.authorizationStatus)")
      Text("Last Location: \(tripRecorder.wrappedLastLocation)")
      Text("Recorded: \(tripRecorder.recordedLocationsCount)")
      Text("Start: \(tripRecorder.wrappedTripStartTime)")
      Text("Distance: \(tripRecorder.wrappedTripDistance)")
      Text("Region: \(tripRecorder.wrappedMonitoredRegion)")
    }
    Text("Last Trip: \(tripRecorder.wrappedLastTripId)")
    Group {
      Text("Motion: \(tripRecorder.motionActivity)")
      Text("Confidence: \(tripRecorder.motionActivityConfidence)")
      Text(tripRecorder.wrappedWillStopRecording)
    }
  }
}

If you want you can add a subscriber to any of the properties of the Recorder. For example the lastTrip property:

let _ = tripRecorder.$lastTrip
.compactMap { $0 }
.sink(receiveValue: { trip in
  print("The last saved trip has the id <\(trip.id)>.")
})

Test your App in the simulator

The MotionSDK runs in the simulator as well as on a real device. So you can just start your app in the simulator and load a gpx file. The recorder will automatically start recording if the provided trip will leave the 100m circular region that the recorder monitors. Since the Simulator cannot simulate CoreMotion it is useful to provide a way to call the stop() function.

Button(action: {
  self.tripRecorder.stop()
}) {
  Text("Stop Recorder")
}

Final

The Recorder publishes its state via Combine-Publishers. You don’t need SwiftUI but Combine and thus iOS 13 is a requirement.

MotionSDKSampleApp.zip