How to use : HKUnit
Sample type Unit type Unit name Unit string Heart Rate count/time Beats per Minute "count/min”
In Swift 3:
func saveHeartRate(date: Date = Date(), heartRate heartRateValue: Double, completion completionBlock: @escaping (Bool, Error?) -> Void) {
let unit = HKUnit.count().unitDivided(by: HKUnit.minute())
let quantity = HKQuantity(unit: unit, doubleValue: heartRateValue)
let type = HKQuantityType.quantityType(forIdentifier: .heartRate)!
let heartRateSample = HKQuantitySample(type: type, quantity: quantity, start: date, end: date)
self.healthKitStore.save(heartRateSample) { (success, error) -> Void in
if !success {
print("An error occured saving the HR sample \(heartRateSample). In your app, try to handle this gracefully. The error was: \(error).")
}
completionBlock(success, error)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With