as i want to tract activity of iphone user using core motion framework , guide me through .
Here's some sample code using a Data Model and SwiftUI
**** The Data Model
import Foundation
import CoreMotion
class DataModel : ObservableObject {
var activityManager : CMMotionActivityManager?
@Published var activities = [CMMotionActivity] () // (remove the space before the ()
init() {
if !CMMotionActivityManager.isActivityAvailable() {
print("Activity Monitoring not available")
return
}
activityManager = CMMotionActivityManager()
if activityManager == nil { // check just to be sure initialised OK
print("Unable to initialise Activity Manager")
return
}
activityManager!.startActivityUpdates(to: OperationQueue.main) { (motion) in
guard let newMotion = motion else { return }
self.activities.append(newMotion)
}
}
}
extension CMMotionActivity {
func activityString() -> String {
// returns a compound string of activities e.g. Stationary Automotive
var output = ""
if stationary { output = output + "Stationary "}
if walking { output = output + "Walking "}
if running { output = output + "Running "}
if automotive { output = output + "Automotive "}
if cycling { output = output + "Cycling "}
if unknown { output = output + "Unknown "}
return output
}
}
**** The Main App (if generated in Xcode 12+)
import SwiftUI
struct Activity_TrackerApp: App {
@StateObject var dataModel = DataModel()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(dataModel)
}
}
}
**** The Content View
import SwiftUI
struct ContentView: View {
@EnvironmentObject var dataModel: DataModel
var body: some View {
ScrollView {
ForEach(dataModel.activities, id:\.startDate) {activity in
HStack{
Text(activity.startDate, style: .date)
Text(activity.startDate, style: .time)
Text(activity.activityString())
}
}
}
}
}
You must also add a Plist key for Privacy - Motion Usage Description and say why you're recording activity.
This is a very rough UI, but it should get you started. You'll also need (??) to turn off monitoring at some point and deal with entering and exiting background mode.
Good luck and regards, Michaela