2016-04-29 41 views
1

我正在研究Research Kit和Health Kit。在模擬器上測試明智我沒有任何數據 - 大量問題:)iOS:如何將多個日期數據添加到Health Kit Store中

如何將數據(最具體的說,步驟)到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)") 
     } 
    }) 
} 

回答

0

忽略我這似乎工作。你需要進入「健康數據」選項卡,並基本選擇你以後的任何事情(在這種情況下,健身>步驟),它會顯示你所有的數據!

您也可以選擇要在儀表板上顯示的選項卡,以便應用程序的初始啓動不會顯示爲空!

我會在這裏給任何想要一個小代碼示例的人+如果他們遇到沒有意識到數據可以顯示的地方!