如何以与 Health 应用中相同的格式检索 Healthkit 记录数据?

问题描述 投票:0回答:1

我正在尝试寻找返回与健康应用程序的“所有记录数据”格式相同的healthkit数据的API。我在下面附上了一张图片。我们目前正在使用 HKStatisticsCollectionQuery,但存在差异。相反,我想向您提供 Apple 在这里提供的确切步骤数据格式。谢谢你。

All recorded data sample

ios healthkit
1个回答
0
投票

这是一个通用的样本获取器;

func getDataSamples(dataType : HKQuantityTypeIdentifier, startDate : Date, endDate : Date, completion: @escaping (Int) -> Void)
{

     var searchPredicate : NSPredicate?
     searchPredicate   = HKQuery.predicateForSamples(withStart: startDate.date, end: endDate.date, options: .strictStartDate)

    let requestedQuantityType = HKObjectType.quantityType(forIdentifier: dataType)!
    let sampleQuery = HKSampleQuery(sampleType: requestedQuantityType, predicate: searchPredicate, limit: limit, sortDescriptors: [])
    {
        (query, result, error) in

        if error != nil
        {
            completion(-1)
        }
        hkSampleRecs.removeAll(keepingCapacity: true)
       
        if result != nil
        {
            for r in result!
            {
                self.hkSampleRecs.append(r)
            }
        }
        completion(self.hkSampleRecs.count)
    }
    
    healthStore.execute(sampleQuery)
}

这样称呼它;

 getDataSamples("HKQuantityTypeIdentifierStepCount", startDate : myStartDate, endDate : myEndDate) 
 {
     resultCount in


        ---- use the results here ----
 }

你必须像这样声明 hkSampleRecs;

 var hkSampleRecs = [HKSample]()
© www.soinside.com 2019 - 2024. All rights reserved.