如何获取之前日期的HealthKit总步数

时间:2017-06-20 08:19:31

标签: ios swift swift3 apple-watch health-kit

我正在尝试从Health Kit获取步骤,它适用于移动设备,但是当我连接Apple Watch时,我的应用会获得更多步骤,然后Health kit。我追踪它并发现它收集了步骤的详细记录,但总体步骤不如健康套件中的详细信息。

我的应用程序得到了这些步骤的总和:

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)
}

1 个答案:

答案 0 :(得分:1)

您应该使用HKStatisticsQueryHKStatisticsCollectionQuery代替HKSampleQuery。统计查询将对来自不同来源的重叠步骤样本进行去除,以确保您不会对它们进行重复计算。您可以找到他们的文档herehere