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

Posted

技术标签:

【中文标题】如何获取以前日期的 HealthKit 总步数【英文标题】:How to get HealthKit total steps for previous dates 【发布时间】:2017-06-20 08:19:30 【问题描述】:

我正在尝试从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)

【问题讨论】:

Aggregated CMPedometerData (iPhone + Watch total count)的可能重复 【参考方案1】:

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

【讨论】:

HKStatisticsCollectionQuery 完成谢谢:-)

以上是关于如何获取以前日期的 HealthKit 总步数的主要内容,如果未能解决你的问题,请参考以下文章

获取 HealthKit 中每个日期的总步数

如何从 HealthKit 中获取每日平均步数

iOS获取健康步数从加速计到healthkit

如何使用 Swift 从适用于 ios 应用程序的 HealthKit 获取循环持续时间

如何按日期获取 Apple 健康数据?

iOS利用HealthKit框架从健康app中获取步数信息