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()
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?