【发布时间】:2016-04-29 15:39:22
【问题描述】:
我正在研究研究工具包和健康工具包。在模拟器上进行明智的测试我没有任何数据 - 大问题:)
如何将数据(尤其是步骤)导入 HealthKit 存储?下面是我得到的,它似乎没有失败,但 Health 应用程序仪表板中没有显示任何内容。我希望它能够提供 14 天的数据...
var date:NSDate! = NSDate()
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let dateComponents = NSDateComponents()
dateComponents.day = -13
let endingDate:NSDate! = calendar.dateByAddingComponents(dateComponents, toDate: date, options: NSCalendarOptions())
while date.compare(endingDate) != NSComparisonResult.OrderedAscending {
let countRate = Double(arc4random_uniform(500) * arc4random_uniform(10))
let stepsUnit = HKUnit.countUnit()
let quantity = HKQuantity(unit: stepsUnit, doubleValue: countRate)
let dateComponents = NSDateComponents()
dateComponents.day = -1
date = calendar.dateByAddingComponents(dateComponents, toDate: date, options: NSCalendarOptions())
let sample = HKQuantitySample(type: HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)!, quantity: quantity, startDate: date, endDate: date)
HKHealthStore().saveObject(sample, withCompletion: { (success, error) -> Void in
if let _ = error {
print("Error saving sample: \(error!.localizedDescription)")
}
})
}
【问题讨论】: