简体   繁体   English

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

[英]How can I get daily average steps form HealthKit

Im trying to display the daily amount of steps the user takes. 我试图显示用户每天的步数。 But is don't really know how to manage this. 但是真的不知道如何管理这个。

I already got this code: 我已经有了这个代码:

let endDate = NSDate()
let startDate = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitMonth, value: -1, toDate: endDate, options: nil)

let weightSampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)

let query = HKSampleQuery(sampleType: weightSampleType, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: {
        (query, results, error) in
        if results == nil {
            println("There was an error running the query: \(error)")
        }

        dispatch_async(dispatch_get_main_queue()) {
            var dailyAVG = Int()
            var steps = results as [HKQuantitySample]
            for var i = 0; i < results.count; i++
            {
                //results[i] add values to dailyAVG
            }
        }
    })

 self.healthKitStore.executeQuery(query)

The query gets all the data needed as far as i know. 据我所知,查询获取了所需的所有数据。 But i don't know how to get the values out of the HKQuantitySample. 但我不知道如何从HKQuantitySample中获取值。 So I cant test if the correct values are in the HKQuantitySample Array. 所以我不能测试HKQuantitySample数组中是否有正确的值。

In case anyone else is trying to solve this in a not-so-terrible way... 万一其他人试图以一种不那么可怕的方式解决这个问题......

At this time, it doesn't look like using HKStatisticsQuery or HKStatisticsCollectionQuery is possible for getting average step count. 目前,它看起来不像使用HKStatisticsQueryHKStatisticsCollectionQuery可以获得平均步数。 You can only use HKStatisticsOptionDiscreteAverage on a discrete data type. 您只能在离散数据类型上使用HKStatisticsOptionDiscreteAverage If you look at the header for HKTypeIdentifiers , you will see that HKQuantityTypeIdentifierStepCount is a cumulative data type. 如果查看HKTypeIdentifiers的标题,您将看到HKQuantityTypeIdentifierStepCount是累积数据类型。

If you try and fetch the average step count, Xcode spits out: Statistics option HKStatisticsOptionDiscreteAverage is not compatible with cumulative data type HKQuantityTypeIdentifierStepCount 如果您尝试获取平均步数,Xcode会吐出:统计选项HKStatisticsOptionDiscreteAverage与累积数据类型HKQuantityTypeIdentifierStepCount不兼容

Best Solution 最佳方案

Get the total number of steps a user has taken. 获取用户已执行的步骤总数。 You can specify a time period between two dates. 您可以指定两个日期之间的时间段。 Divide the total number of steps by however many days are between your two dates. 将总步数除以两天之间的天数。

You need to loop through steps not results and then use each HKQuantitySample result's quantity property to get the number of steps in that sample, ex: 您需要遍历steps而不是results ,然后使用每个HKQuantitySample结果的quantity属性来获取该样本中的步骤数,例如:

var dailyAVG:Double = 0
for steps in results as [HKQuantitySample]
{
   // add values to dailyAVG
   dailyAVG += steps.quantity.doubleValueForUnit(HKUnit.countUnit())
}

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

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