I'm trying to build an app with swift which will extract the heart rate from iwatch and display it to the user along with it will play some music in the user's iphone. Pretty new to ios ,so im trying to figure out how to extract data from healthkit in iwatch and sync it with the mobile app. Do i need to build 2 apps , one for watch and phone or the same app? and how to integrate health kit since i have just enabled it in the target->capabilities.
Asked
Active
Viewed 1,012 times
-3
-
Possible duplicate : https://stackoverflow.com/questions/35039742/ios-get-heart-rate-from-apple-watch-in-near-real-time – Manav Sep 05 '19 at 07:38
1 Answers
0
public func subscribeToHeartBeatChanges() {
// Creating the sample for the heart rate
guard let sampleType: HKSampleType =
HKObjectType.quantityType(forIdentifier: .heartRate) else {
return
}
/// Creating an observer, so updates are received whenever HealthKit’s
// heart rate data changes.
self.heartRateQuery = HKObserverQuery.init(
sampleType: sampleType,
predicate: nil) { [weak self] _, _, error in
guard error == nil else {
log.warn(error!)
return
}
/// When the completion is called, an other query is executed
/// to fetch the latest heart rate
self.fetchLatestHeartRateSample(completion: { sample in
guard let sample = sample else {
return
}
/// The completion in called on a background thread, but we
/// need to update the UI on the main.
DispatchQueue.main.async {
/// Converting the heart rate to bpm
let heartRateUnit = HKUnit(from: "count/min")
let heartRate = sample
.quantity
.doubleValue(for: heartRateUnit)
/// Updating the UI with the retrieved value
self?.heartRateLabel.setText("\(Int(heartRate))")
}
})
}
}
public func fetchLatestHeartRateSample(
completion: @escaping (_ sample: HKQuantitySample?) -> Void) {
/// Create sample type for the heart rate
guard let sampleType = HKObjectType
.quantityType(forIdentifier: .heartRate) else {
completion(nil)
return
}
/// Predicate for specifiying start and end dates for the query
let predicate = HKQuery
.predicateForSamples(
withStart: Date.distantPast,
end: Date(),
options: .strictEndDate)
/// Set sorting by date.
let sortDescriptor = NSSortDescriptor(
key: HKSampleSortIdentifierStartDate,
ascending: false)
/// Create the query
let query = HKSampleQuery(
sampleType: sampleType,
predicate: predicate,
limit: Int(HKObjectQueryNoLimit),
sortDescriptors: [sortDescriptor]) { (_, results, error) in
guard error == nil else {
print("Error: \(error!.localizedDescription)")
return
}
completion(results?[0] as? HKQuantitySample)
}
self.healthStore.execute(query)
}

Samarjeet dubey
- 9
- 4
-
Instead of just copying and pasting code you should has details of why you think you code will work. – chirag90 Sep 05 '19 at 07:54
-
1I thought proper comments already in the code.This code is working for me – Samarjeet dubey Sep 05 '19 at 08:03
-
-
coz in my case, its saying self.heartRateQuery its says undefined reference to interfacecontroller ... Value of type 'InterfaceController' has no member 'heartRateQuery'; did you mean 'heartRateLabel'? – Gaurav Roy Sep 07 '19 at 05:35