简体   繁体   English

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

[英]How to get HealthKit total steps for previous dates

I am trying to get steps from Health Kit , its working fine for mobile but when i connect Apple Watch my app get more steps then Health kit .我正在尝试从Health Kit获取步骤,它在移动设备上运行良好,但是当我连接Apple Watch我的应用程序获得的步骤比Health kit i trace it and find that it collect detail record of steps but total steps are less then detail in Health kit.我跟踪它并发现它收集了详细的步骤记录,但总步数少于 Health kit 中的详细信息。

My App getting the sum of these steps:我的应用程序获取这些步骤的总和:

在此处输入图片说明

But I want To Get these:但我想得到这些:

在此处输入图片说明

Here is My Code:这是我的代码:

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

You should use HKStatisticsQuery or HKStatisticsCollectionQuery instead of HKSampleQuery .您应该使用HKStatisticsQueryHKStatisticsCollectionQuery而不是HKSampleQuery The statistics queries will de-deuplicate overlapping step samples from different sources to ensure that you do not double-count them.统计查询将对来自不同来源的重叠步骤样本进行去重,以确保您不会重复计算它们。 You can find documentation for them here and here .您可以在此处此处找到它们的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM