2017-06-20 65 views
0

我想從Health Kit獲得步驟,其工作正常,但當我連接Apple Watch我的應用獲得更多步驟,然後Health kit。我跟蹤它,發現它收集了步驟的詳細記錄,但是健康工具箱中的總步驟少於詳細信息。如何獲取以前的日期的HealthKit總步驟

我的應用讓這些步驟的總和:

enter image description here

但我希望得到這些:

enter image description here

這裏是我的代碼:

func MultipleDaysStepsAndActivitiesTest(_ startDate:Date, completion: @escaping (NSDictionary, [HealthKitManuallActivity], NSError?) ->()) { 
    let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount) // The type of data we are requesting 

    let now = Date() 

    let newDate = startDate 

    let predicate = HKQuery.predicateForSamples(withStart: newDate, end: now, options: HKQueryOptions()) 

    var dates = now.datesBetweenGivenDates(startDate,endDate:now) 
    dates = dates.reversed() 

    let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in 

     var dict:[String:Double] = [:] 

     if results?.count > 0 { 

      for result in results as! [HKQuantitySample] { 
       print(result) 
       if result.sourceRevision.source.name != kHealthKitSource { 

        if dict[self.fmt.string(from: result.startDate)] != nil { 
         dict[self.fmt.string(from: result.startDate)] = dict[self.fmt.string(from: result.startDate)]! + result.quantity.doubleValue(for: HKUnit.count()) 

        } else { 
         dict[self.fmt.string(from: result.startDate)] = result.quantity.doubleValue(for: HKUnit.count()) 
        } 
       } 
      } 
     } 

     var sDate = startDate // first date 
     let cal = Calendar.current 
     print(dict) 

     if dict.isEmpty { 

      while sDate <= Date() { 
       dict[self.fmt.string(from: sDate)] = 0 
       sDate = cal.date(byAdding: .day, value: 1, to: sDate)! 
      } 

     } else { 

      while sDate <= Date() { 

       if dict[self.fmt.string(from: sDate)] == nil { 
        dict[self.fmt.string(from: sDate)] = 0 
       } 

       sDate = cal.date(byAdding: .day, value: 1, to: sDate)! 
      } 
     } 

     // reading activities 
     self.MultipleDaysWorkouts(startDate, endDate: now, completion: { (activities, error) in 

      if results?.count == 0 { 

       for activity in activities { 
        dict[activity.startDate] = 0.0 
       } 
      } 

      // reading mindfulness activities 

      self.MultipleDayMindFullnessActivity(startDate, completion: { (mindfulnessActivities, mindError) in 

       if mindError == nil { 

        let allActivities = mindfulnessActivities + activities 
        completion(dict as NSDictionary, allActivities, mindError as NSError?) 

       } 

      }) 

     }) 

    } 

    execute(query) 
} 
+0

[Aggregated CMPedometerData(iPhone + Watch total count)](https://stackoverflow.com/questions/44580730/aggregated-cmpedometerdata-iphone-watch-total-count) –

回答

1

你應該使用HKStatisticsQueryHKStatisticsCollectionQuery而不是HKSampleQuery。統計信息查詢將去除不同來源的重疊步驟樣本,以確保您不會對它們進行重複計數。你可以找到它們的文檔herehere

+0

可能與「HKStatisticsCollectionQuery」完成謝謝:-) –

相關問題