利用 Core Motion 与 Fitbit API 实现健身数据管理
1. 使用 Core Motion 保存运动数据
在开发健身应用时,Core Motion 框架能帮助我们获取设备的运动数据,结合 HealthKit 可以实现数据的存储和管理。以下将详细介绍如何使用 Core Motion 进行运动数据的记录和保存。
1.1 开始锻炼
在 CreateWorkoutViewController.swift
中,我们可以实现开始锻炼的功能。代码如下:
func startWorkout() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "updateTime", userInfo: nil, repeats: true)
if initialStartDate == nil {
initialStartDate = NSDate()
}
startDate = NSDate()
//start counting steps
toggleButton.backgroundColor = UIColor.redColor()
toggleButton.setTitle("Pause workout", forState: UIControlState.Normal)
if (CMMotionActivityManager.isActivityAvailable() &a