查询Healthkit应该返回一个包含步数,距离和卡路里的数组对象。
我能够通过
分别归还它们
活动步骤/小时
func activitySteps(_ startDate:NSDate, endDate:NSDate, anchorDate:NSDate, completion: @escaping (Array<NSObject>, NSError?) -> ()) {
let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)
let interval = NSDateComponents()
interval.hour = 1
let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: endDate as Date, options: .strictEndDate)
let query = HKStatisticsCollectionQuery(quantityType: type!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: anchorDate as Date, intervalComponents:interval as DateComponents)
query.initialResultsHandler = { query, results, error in
if let myResults = results{
var stepsArray: [NSObject] = []
myResults.enumerateStatistics(from: startDate as Date, to: endDate as Date) {
statistics, stop in
if let quantity = statistics.sumQuantity() {
let steps = quantity.doubleValue(for: HKUnit.count())
// print("\(statistics.endDate.timeIntervalSince1970): steps = \(steps)")
let ret = [
"steps": steps,
"startDate" : statistics.startDate.timeIntervalSince1970 * 1000.0,
"endDate": statistics.endDate.timeIntervalSince1970 * 1000.0
] as [String : Any]
stepsArray.append(ret as NSObject)
}
}
completion(stepsArray, error as NSError?)
}
}
healthKitStore.execute(query)
}
活动距离/小时
func activityDistance(_ startDate:NSDate, endDate:NSDate, anchorDate:NSDate, completion: @escaping (Array<NSObject>, NSError?) -> ()) {
let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)
let interval = NSDateComponents()
interval.hour = 1
let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: endDate as Date, options: .strictEndDate)
let query = HKStatisticsCollectionQuery(quantityType: type!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: anchorDate as Date, intervalComponents:interval as DateComponents)
query.initialResultsHandler = { query, results, error in
if let myResults = results{
var distanceArray: [NSObject] = []
myResults.enumerateStatistics(from: startDate as Date, to: endDate as Date) {
statistics, stop in
if let quantity = statistics.sumQuantity() {
let distance = quantity.doubleValue(for: HKUnit.meter())
print("\(statistics.endDate.timeIntervalSince1970): distance = \(distance)")
let ret = [
"distance": distance,
"startDate" : statistics.startDate.timeIntervalSince1970 * 1000.0,
"endDate": statistics.endDate.timeIntervalSince1970 * 1000.0
] as [String : Any]
distanceArray.append(ret as NSObject)
}
}
completion(distanceArray, error as NSError?)
}
}
healthKitStore.execute(query)
}
活动卡路里/小时
func activityCalories(_ startDate:NSDate, endDate:NSDate, anchorDate:NSDate, completion: @escaping (Array<NSObject>, NSError?) -> ()) {
let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)
let interval = NSDateComponents()
interval.hour = 1
let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: endDate as Date, options: .strictEndDate)
let query = HKStatisticsCollectionQuery(quantityType: type!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: anchorDate as Date, intervalComponents:interval as DateComponents)
query.initialResultsHandler = { query, results, error in
if let myResults = results{
var caloriesArray: [NSObject] = []
myResults.enumerateStatistics(from: startDate as Date, to: endDate as Date) {
statistics, stop in
if let quantity = statistics.sumQuantity() {
let calories = quantity.doubleValue(for: HKUnit.kilocalorie())
print("\(statistics.endDate.timeIntervalSince1970): calories = \(calories)")
let ret = [
"calories": calories,
"startDate" : statistics.startDate.timeIntervalSince1970 * 1000.0,
"endDate": statistics.endDate.timeIntervalSince1970 * 1000.0
] as [String : Any]
caloriesArray.append(ret as NSObject)
}
}
completion(caloriesArray, error as NSError?)
}
}
healthKitStore.execute(query)
}
我想将所有这些组合到一个对象中。
[
{
steps: 70,
distance:10,
calories: 200,
startDate: ...,
endDate: ...,
},...
]
是否可以直接从HealthKit本身以每小时间隔的速率生成以上格式的数据? 如果不可能的话,如何将所有3个数据合并到一个对象中,数据的范围可以是一天中的一个月,一个月,一个月。