我正在尝试寻找返回与健康应用程序的“所有记录数据”格式相同的healthkit数据的API。我在下面附上了一张图片。我们目前正在使用 HKStatisticsCollectionQuery,但存在差异。相反,我想向您提供 Apple 在这里提供的确切步骤数据格式。谢谢你。
这是一个通用的样本获取器;
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]()