I have been successfully able to write to HealthKit but receiving these values always return 0. I am trying to return the latest value of weight.
This is the function that I read weight:
public func readWeight(result: @escaping (Double) -> Void) {
print("Weight")
let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)
let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) {
query, results, error in
if (error != nil) {
print(error!)
result(0.0)
}
guard let results = results else {
print("No results of query")
result(0.0)
return
}
if (results.count == 0) {
print("Zero samples")
result(0.0)
return
}
guard let bodymass = results[0] as? HKQuantitySample else {
print("Type problem with weight")
result(0.0)
return
}
result(bodymass.quantity.doubleValue(for: HKUnit.pound()))
}
healthKitStore.execute(weightQuery)
}
This is how I set it to a variable:
readWeight() { weight in
Weight = weight
}
Thanks!