gpt4 book ai didi

swift - 如何按日期明智地获取Apple健康数据?

转载 作者:IT王子 更新时间:2023-10-29 05:37:14 26 4
gpt4 key购买 nike

Apple 健康应用程序按日期提供数据,如下图所示。

enter image description here

通过使用 HealthKit 我正在从 apple health 获取步数数据作为

let p1 = HKQuery.predicateForSamples(withStart: fromDate, end: Date(), options: .strictStartDate)
let p2 = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyWasUserEntered, operatorType: .notEqualTo, value: true)
let timeSortDesriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)//as in health kit entry
let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
let predicate = HKQuery.predicateForSamples(withStart: fromDate, end: Date(), options: .strictStartDate)
let sourceQuery = HKSourceQuery(sampleType: quantityType, samplePredicate: predicate, completionHandler: { query,sources,error in

if sources?.count != 0 && sources != nil {

let lastIndex = sources!.count - 1
var sourcesArray = Array(sources!)
for i in 0..<sourcesArray.count {
let sourcePredicate = HKQuery.predicateForObjects(from: sourcesArray[i])
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [p1, p2,sourcePredicate])
let query = HKSampleQuery(sampleType: quantityType, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: [timeSortDesriptor], resultsHandler: { query, results, error in
guard let samples = results as? [HKQuantitySample] else {
return
}......

sourceQuery 提供多个对象,例如 Apple Watch、My iPhone。此外,我将 for 循环与 HKSampleQuery 一起使用,它给出了 HKQuantitySample 对象。问题是 [HKQuantitySample] 给出了未按日期排序的步骤数据数组。我正在寻找日期俱乐部的数据,例如苹果健康应用程序中显示的内容。

是的,有解决方法,例如按日期手动对 [HKQuantitySample] 中的数据进行排序。但可能有使用 predicates 或其他方法的解决方法。如果您需要任何额外信息,请随时询问。

编辑:根据@Allan 的建议我添加了HKStatisticsCollectionQuery , 是的,它提供数据 datewise 但接收的步数与 Apple health App 中的不同。下面的代码需要添加/修改什么吗?

let last10Day = Calendar.current.date(byAdding: .day, value: -10, to: Date())!
var interval = DateComponents()
interval.day = 1
let quantityType1 = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
// Create the query
let query = HKStatisticsCollectionQuery(quantityType: quantityType1,
quantitySamplePredicate: nil,
options: .cumulativeSum,
anchorDate: last10Day,
intervalComponents: interval)
// Set the results handler
query.initialResultsHandler = {
query, results, error in

guard let statsCollection = results else {
// Perform proper error handling here
fatalError("*** An error occurred while calculating the statistics: \(String(describing: error?.localizedDescription)) ***")
}
let endDate = Date()
statsCollection.enumerateStatistics(from: last10Day, to: endDate, with: { (statistics, stop) in
if let quantity = statistics.sumQuantity() {
let date = statistics.startDate
let value = quantity.doubleValue(for: HKUnit.count())
print("--value-->",value, ",--For Date-->",date)
}
})
}
healthStore.execute(query)

最佳答案

使用HKStatisticsCollectionQuery 获取特定时期的数据。下面是一个示例,展示了如何获取过去 30 天的步数:

private let healthStore = HKHealthStore()
private let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!

func importStepsHistory() {
let now = Date()
let startDate = Calendar.current.date(byAdding: .day, value: -30, to: now)!

var interval = DateComponents()
interval.day = 1

var anchorComponents = Calendar.current.dateComponents([.day, .month, .year], from: now)
anchorComponents.hour = 0
let anchorDate = Calendar.current.date(from: anchorComponents)!

let query = HKStatisticsCollectionQuery(
quantityType: stepsQuantityType,
quantitySamplePredicate: nil,
options: [.cumulativeSum],
anchorDate: anchorDate,
intervalComponents: interval
)
query.initialResultsHandler = { _, results, error in
guard let results = results else {
log.error("Error returned form resultHandler = \(String(describing: error?.localizedDescription))")
return
}

results.enumerateStatistics(from: startDate, to: now) { statistics, _ in
if let sum = statistics.sumQuantity() {
let steps = sum.doubleValue(for: HKUnit.count())
print("Amount of steps: \(steps), date: \(statistics.startDate)")
}
}
}

healthStore.execute(query)
}

关于swift - 如何按日期明智地获取Apple健康数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50620547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com