【问题标题】:iOS: How to add multiple day data into Health Kit StoreiOS:如何将多天数据添加到 Health Kit Store
【发布时间】: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)")
        }
    })
}

【问题讨论】:

    标签: ios swift healthkit


    【解决方案1】:

    忽略我,这似乎有效。您需要进入“健康数据”选项卡并基本上选择您所追求的任何内容(在本例中为“健身”>“步骤”),它会向您显示所有数据!

    您也可以选择要在仪表板上显示的选项卡,这样应用程序的初始启动就不会看起来是空的!

    我会把这个留给任何想要一个迷你代码示例的人+如果他们没有意识到数据可以显示在哪里!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      相关资源
      最近更新 更多