繁体   English   中英

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

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

我正在尝试从Health Kit获取步骤,它在移动设备上运行良好,但是当我连接Apple Watch我的应用程序获得的步骤比Health kit 我跟踪它并发现它收集了详细的步骤记录,但总步数少于 Health kit 中的详细信息。

我的应用程序获取这些步骤的总和:

在此处输入图片说明

但我想得到这些:

在此处输入图片说明

这是我的代码:

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

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

暂无
暂无

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

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