How do I use motion and pedometer data in Swift?

Integrating motion and pedometer data into your Swift applications can enhance user experience by tracking physical activity and providing valuable insights. Below is a simple example of how to use the Core Motion framework to access pedometer data in Swift.

import CoreMotion class PedometerManager { let pedometer = CMPedometer() func startTracking() { if CMPedometer.isStepCountingAvailable() { pedometer.startPedometerUpdates(from: Date()) { (data, error) in guard let data = data, error == nil else { print("Error: \(String(describing: error))") return } print("Steps: \(data.numberOfSteps)") print("Distance: \(data.distance ?? 0) meters") } } else { print("Step counting is not available.") } } func stopTracking() { pedometer.stopPedometerUpdates() } } // Usage let pedometerManager = PedometerManager() pedometerManager.startTracking()

Core Motion Pedometer Data Swift iOS Development Step Counting Fitness Tracking