Health & Fitness

RSS for tag

Explore the technical aspects of health and fitness features, including sensor data acquisition, health data processing, and integration with the HealthKit framework.

Health & Fitness Documentation

Post

Replies

Boosts

Views

Activity

HealthKit Background execution
Hi All, I have a strange issue. I am using enableBackgroundDelivery for updating user step count in background mode using health kit. It works fine when I execute the app by pressing 'Run' in xcode. But the code is not triggering when I am directly launching it on my device. I have tried many different things but cannot figure out the issue from 2 days 😭. I would really appreciate any suggestions. Thanks
1
0
416
Apr ’24
HealthKit StatisticsCollectionQuery InitialResultsHandler not called
Hello, I am writing as I am seeing a very strange behavior when attempting to run an HKStatisticsCollectionQuery over multiple app starts. Steps: Initially load and launch my app into an iOS simulator (running iOS 17.2) via Xcode (version 15.3). Execute code path which invokes the following method below. Authorize necessary Read permission for the HKQuantityTypeIdentifierStepCount type. Observed that a non-nil HKStatisticsCollection is returned in the initialResultsHandler, with corresponding expected datapoints. These datapoints are in no way stored or retained on the device at all. Stop the app via Xcode. Relaunch the app via Xcode. Execute the same code path as in step 2. Observed (via breakpoints) that even though the query is executed by the HKHealthStore, the initialResultsHandler is never called and no HKStatisticsCollection is ever returned. Input parameters are the same in both instances of the call, and can confirm there is data in the devices HealthKit datastore. I would expect, reading Apple's documentation, that both queries should execute and return the exact same datapoints in an HKStatisticsCollection, but please feel free to correct me if I am misunderstanding something, or if my code is incorrect in some way. func fetchStatisticsCollection(with quantityType: HKQuantityType, predicate: NSPredicate? = nil, options: HKStatisticsOptions, anchorDate: Date? = nil, interval: DateComponents? = nil, initialResultsCompletion: @escaping (Result<HKStatisticsCollection, Error>) -> Void, updateHandler: (Result<HKStatisticsCollection, Error>) -> Void?) { var statisticsQueryAnchorDate: Date = Date() if let anchor = anchorDate { statisticsQueryAnchorDate = anchor } else if let yesterdayAnchor = createStaticsQueryAnchorDateYesterday(){ statisticsQueryAnchorDate = yesterdayAnchor } let dateInterval = interval ?? DateComponents(day: 1) // Create the query let query = HKStatisticsCollectionQuery(quantityType: quantityType, quantitySamplePredicate: predicate, options: options, anchorDate: statisticsQueryAnchorDate, intervalComponents: dateInterval) // Set the results handler query.initialResultsHandler = { query, results, error in if let error = error as? HKError{ // Return wrapped HKError initialResultsCompletion(.failure(HealthKitManagerError.hkError(error: error))) return } guard let statsCollection = results else { // Return custom Empty Results error. initialResultsCompletion(.failure(HealthKitManagerError.emptyResults)) return } initialResultsCompletion(.success(statsCollection)) } if let updateHandler = updateHandler { query.statisticsUpdateHandler = { query, statistics, statisticsCollection, error in if let error = error as? HKError{ // Return custom wrapped HKError. updateHandler(.failure(HealthKitManagerError.hkError(error: error))) return } guard let statsCollection = statisticsCollection else { // Return custom Empty Results error. updateHandler(.failure(HealthKitManagerError.emptyResults)) return } updateHandler(.success(statsCollection)) } } if activeTypeQueries[quantityType.identifier] == nil { healthStore.execute(query) activeTypeQueries[quantityType.identifier] = query healthStore.enableBackgroundDelivery(for: quantityType, frequency: .immediate) { (success, error) in if let error = error { return } if success { print("background enabled") } } } else { print("NOT executing query, we already have one query running for this type") } } Thank you
0
1
311
Apr ’24
Remote device unreachable
Hello, I am using the project from https://developer.apple.com/documentation/healthkit/workouts_and_activity_rings/building_a_multidevice_workout_app The workout is starting on the watch and I can see the metrics, but it seems the mirroring with the phone is not working, I am getting this errors [mirroring] <HKWorkoutSession:0x600002c10100 57C340BA-2AA9-4477-9AC8-181698A526A5 running [Primary]>: Failed to send data to remote session with error: Error Domain=com.apple.healthkit Code=300 "Remote device is unreachable" UserInfo={NSLocalizedDescription=Remote device is unreachable, NSUnderlyingError=0x600000c53b70 {Error Domain=RPErrorDomain Code=-6727 "kNotFoundErr ('rapport:rdid:PairedCompanion' not found)" UserInfo={cuErrorDesc=kNotFoundErr ('rapport:rdid:PairedCompanion' not found), cuErrorMsg='rapport:rdid:PairedCompanion' not found, NSLocalizedDescription=kNotFoundErr ('rapport:rdid:PairedCompanion' not found)}}} Does anybody else has this issue? Thank you
0
0
289
May ’24
How to add "Estimated Time in Each Heart Rate Zone" info to my custom HKWorkout when viewed in Apple Fitness app?
Hello, I’m currently developing a fitness app for watchOS that lets a user to manually set a desired heart rate target zone (enter numbers representing the lower and upper boundaries) and start a workout (right now it’s only “Other” type). After that my app monitors user’s heart rate and alerts them when they’re out of zone. When user ends the workout, the info about this workout appears on “Fitness” iOS app, and user can see the workout data like Workout Time, Active and Total Calories, Avg. Heart Rate. Also user can see Heart Rate chart with info how their heart rate was changing during a workout (see the Figure 1). Now to the question. When user clicks “Show More” button above the Heart Rate chart, they can see the same Heart Rate chart and another one, with Post-Workout Heart Rate (see the Figure 2). But there is no “Estimated time in each heart rate zone” as one can see in the workout’s details that were recorded from Apple’s workout (watchOS “Workout” app, for a workout of “Other” type as well). Please see the Figure 3. The question is: is it possible to add “Estimated time in each heart rate zone” to workout recorded via my third-party app so it would look like on the Figure 3 in "Fitness" iOS app, and if it's possible, what steps should I undertake to implement this ? Thanks in advance! I posted the screenshots in the replies to the post, because otherwise I was not able to submit a post ("sensitive language" warning, I suspect it's because of the ids in the attached screenshot's urls)
1
0
396
May ’24
Delayed reports from CMFallDetectionManager
Our watchOS app uses CMFallDetectionManager (with the proper entitlement) to help alert our users and their caregivers when a fall occurs. We have had a simple implementation in our app for a couple of years now and it seems to work fine. Recently, we received a report of delayed fall alerts and have traced back the root cause to a failure from the system to call func fallDetectionManager(_ fallDetectionManager: CMFallDetectionManager, didDetect event: CMFallDetectionEvent, completionHandler handler: @escaping () -> Void) promptly when a fall occurs. Our implementation of this method begins with the following logging statement: "Fall detected at \(event.date) with status \(event.resolution.rawValue) at \(Date())" When we check our logs, we see a number of events that occur as expected, for example: Fall detected at DATE_AND_HOURS:42:09 +0000 with status 1 at DATE_AND_HOURS:42:51 +0000 However, many events show a period of several minutes from fall detection to a report: Fall detected at DATE_AND_HOURS:28:09 +0000 with status 0 at DATE_AND_HOURS:32:42 +0000 or Fall detected at DATE_AND_HOURS:44:26 +0000 with status 1 at DATE_AND_HOURS:48:14 +0000 And in the instance from our customer, we had a fall detected that evidently wasn't reported for 5 hours and 4 minutes (I'm not sharing exact timestamps publicly to maintain user privacy). We are aware of the documentation regarding the delegate and have programmed it appropriately not to receive duplicate events: Additionally, each time the user launches your app, the system checks to see if a fall event has occurred during the recent past. If one has occurred, it calls this method. Note that your app may receive the same event multiple times, for example, if the app crashes and relaunches. Always check the event’s date property to determine whether your app has already received the event. The system guarantees that different fall events have different date values. Moreover, our logger persists logs when there is no network access, and the delegate callback also requests a network post to our servers that, again, is preserved in a background queue until connectivity is restored if it's not available. Our app's other updates (watchOS complication, etc.) from this user's Watch also show that our app was running in the background and communicating with our servers during that time. We have very high confidence that the watch and our app did not miss any more timely calls to that delegate method. While this one five-hour delay was an exceptionally bad occurrence, we also find the other delays of several minutes to be concerning, considering the time-sensitive nature of falls. Does Apple or do any developers have any insights about what's going on and what expectations we should be setting for our users?
9
0
528
May ’24
Seeking Advice on Real-Time Heartbeat Data from Apple Watch: Heart Rate vs. HKElectrocardiogram
Hello, I am developing an Apple Watch app in Swift and SwiftUI that needs to receive real-time heartbeat data to visualize the time intervals between heartbeats. The app draws circles on the screen where the size and position of each circle are based on the time interval between consecutive heartbeats. I am currently using the HKQuantityType for .heartRate from HealthKit to get heart rate data and calculate the intervals. However, I am wondering if this is the best approach for my requirement. I came across the HKElectrocardiogram class, and I am not sure if it would be a better fit for obtaining real-time heartbeat intervals. My questions are: Real-Time Heartbeats: Is HKQuantityType for .heartRate the most appropriate way to get real-time heartbeat data for calculating intervals between beats? Can HKElectrocardiogram provide real-time heartbeat intervals, or is it more suited for detailed ECG recordings rather than instantaneous heartbeats? Accuracy and Performance: Which method provides the most accurate and real-time data for heartbeat intervals? Are there any other APIs or services in the Apple Watch ecosystem that I should consider for this purpose? Best Practices: What are the best practices for implementing real-time heartbeat monitoring in an Apple Watch app? Are there any sample projects or documentation that could help me understand the optimal way to achieve this? Here is a brief overview of my current implementation using HKQuantityType for .heartRate: import Foundation import HealthKit class HeartRateMonitor: NSObject, ObservableObject { @Published var heartRate: Double = 0.0 @Published var intervals: [TimeInterval] = [] private var lastHeartRateTimestamp: Date? private var healthStore: HKHealthStore? private let heartRateQuantityType = HKObjectType.quantityType(forIdentifier: .heartRate) private let appStartTime: Date override init() { self.appStartTime = Date() super.init() if HKHealthStore.isHealthDataAvailable() { self.healthStore = HKHealthStore() self.requestAuthorization() } } private func requestAuthorization() { guard let heartRateQuantityType = self.heartRateQuantityType else { return } healthStore?.requestAuthorization(toShare: nil, read: [heartRateQuantityType]) { success, error in if success { self.startMonitoring() } } } func startMonitoring() { guard let heartRateQuantityType = self.heartRateQuantityType else { return } let query = HKAnchoredObjectQuery( type: heartRateQuantityType, predicate: nil, anchor: nil, limit: HKObjectQueryNoLimit) { (query, samples, deletedObjects, newAnchor, error) in guard let samples = samples as? [HKQuantitySample] else { return } self.process(samples: samples) } query.updateHandler = { (query, samples, deletedObjects, newAnchor, error) in guard let samples = samples as? [HKQuantitySample] else { return } self.process(samples: samples) } healthStore?.execute(query) } private func process(samples: [HKQuantitySample]) { for sample in samples { if sample.endDate > appStartTime { let heartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute()) let heartRate = sample.quantity.doubleValue(for: heartRateUnit) DispatchQueue.main.async { self.heartRate = heartRate if let lastTimestamp = self.lastHeartRateTimestamp { let interval = sample.endDate.timeIntervalSince(lastTimestamp) self.intervals.append(interval) } self.lastHeartRateTimestamp = sample.endDate } } } } } Thank you for your guidance and suggestions!
1
0
436
Jun ’24
Any changes in HRV sample frequency in watchOS 11?
Are there any changes in the frequency of HRV samples the Apple Watch takes in watchOS 11? Currently sample rates are very low except for users who have Afib History turned on. There should be some other way for users to get the benefit of more HRV sampling per day without needing to turn Afib History on if they haven't been diagnosed with Afib. Also is it possible for a 3rd party app to trigger an HRV sample? Thanks! Gary
2
1
409
Jun ’24
Which workouts have a GPS track?
The session Build custom swimming workouts with WorkoutKit mentions Outdoor activities supporting distance goals and lists: Cycling Running Hiking Walking Swimming Wheelchair walk + run And new: Golf Rowing Downhill skiing Snowboarding Lacrosse Hockey Disc Sports Skating Paddle sports Soccer American football Australian football Rugby Cross country skiing I know the previous 6 have GPS tracks saved with the workout in Health. Do all of the other ones now also save the GPS track to Health? If not all of them, which do?
1
0
296
Jun ’24
Wellbeing APIs Missing Documentation?
In the Explore Wellbeing APIs in HealthKit video, developers are referred to the documentation for more information. However, like most APIs in recent years, there doesn't seem to be any documentation. Can someone please clarify what specifically the video references? Explore Wellbeing APIs in HealthKit: First, let’s start with some terminology. The GAD-7 and PHQ-9 are standardized questionnaires that are made available by Pfizer. They’re used by doctors and clinicians around the world for mental health screening. The GAD-7 consists of 7 questions to assess a person’s risk of anxiety and the PHQ-9 consists of 9 questions to assess risk of depression. This year, you’ll be able to read and write the results of the GAD-7 anxiety and PHQ-9 depression questionnaires. With these new data types, people can check in on efficacy of treatment, or save the results from their doctor's office. It’s important to give these assessments in accordance with Pfizer’s standards, so check out our developer documentation to learn more.
1
0
280
Jun ’24
Background Health Store Access for Lock Screen Widgets
It's fairly well know and stated that the Apple Health / HealthKit data store is unavailable when iPhone is locked. Since Lock Screen Widgets were introduced there's been a feature parity mismatch with Apple's own Fitness app which is able to display updating Activity Rings on the Lock Screen. Third party apps cannot do this and have to rely unlocking their device to then trigger an update. This means they often display stale and wrong Health data. With the release of iOS 18 beta, I see no changes to this... Is there anything I've missed? Currently for requesting the Timeline Updates on my Widget I have to just keep requesting updates as often as possible and hope that each time the iPhone might be unlocked.... This is inefficient and a waste of device resources. Even a Widget timeline reload API that let the developer say "Only call update if iPhone unlocked" would be useful.
3
1
311
Jun ’24
Is there a way to know users have enabled pausing their Activity Rings?
Hi everyone, With iOS 18 and watchOS 11 it is possible to pause your activity rings so that a streak is not broken when you are ill or something like that. When we request data from the HealthStore, is there a way to know that a user has set this option for a particular date or period? Or will the data for that date just return nil for all 3 ring values or even skip the day in the dataset? I have an app in which I also keep track of people's streaks, so it would really useful to know if the activity rings are paused. Thanks in advance!
1
0
281
Jun ’24
Apple Watch HealthKit Queries Beyond 7 Days?
This has long been a frustration of mine on Apple Watch, and I've brought it up at WWDC labs every year to no avail but while the limitation on the Apple Watch to only be able to query 7 days of HealthKit data may have made sense early in the AW's lifetime for battery preservation etc., it really no longer makes sense in modern AW development. Especially not with stand alone watch apps, as a developer I am prohibited from building the same experience on the Apple Watch that I can in an iPhone app. For example, my app looks at your current health metrics and compares them to your 60 day baseline to identify any deviations from your normal ranges. I cannot create the same experience on the watch using purely HealthKit queries, and am limited to using either iCloud or Watch Connectivity which have their own drawbacks and are not an option for a stand alone watch app. Can we finally expand the length of HealthKit queries the AW can make so that we can build the same powerful experience on the watch that we can on iPhone? Thank you! Gary See also Feedback FB7649612 (from April 2020).
1
0
286
Jun ’24
Writing Workout Data into HealthKit
Hello, I am writing workout data into HealthKit using the HKWorkoutBuilder API for specific types of workouts (e.g. Cycling) in particular various time series data for power, speed, etc.. but I am unable to see the average values for these quantities on the workout summary page in the Apple Fitness app ? (I am only able to see the graph data in the show more / workout details) anything specific to be done in order to have the data shown in the main summary ?
1
0
140
1w
Watch not detecting stands
During WatchOS 10 dev betas and now into 11 dev betas, I am still seeing were stands are not being detected. What I do not know is if the steps are detected. But here is the situation. Today: I was going on some wiring outside and in the basement. I was clearly up and moving around for over an hour. I can see on my camera's I went outside at 15:50, came back in at 16:18. Then went to my basement and was working down there from 16:19 until 16:30. After that cleaned up my mess. So I was up and down stairs and without a doubt standing. At 16:50 I got the reminder to stand. Other cases: 1: I get home from work about XX:10 . I have to walk about 50 ft from my garage to my house, up stairs, take the dog out, make dinner, and finally get my chair to eat and at XX:50 I get the reminder. 2: I'm at work, I'm going to get lunch and I have to walk pretty far and again at XX:50 I get the reminder. This is very rare, but it has happened. Now, in a odd twist. I get up in the morning at XX:50, I have 10 steps to the bathroom, and I manage to get that hourly stand.
0
0
101
3d
Step Data Duplication Issue with Apple Health Integration
We are integrating Apple Health step data into our mobile application, Diyetkolik. However, we have received feedback from our users indicating that the step data appears to be significantly higher than expected. After conducting thorough research, we discovered that users who wear a smartwatch are experiencing step data duplication. Both the smartwatch and the phone's step data are being collected and combined, leading to inflated step counts in our application. We kindly request your assistance in addressing the following issues: How can we differentiate between step data from the smartwatch and the phone to avoid duplication? Are there best practices or specific APIs we should use to filter and manage step data more accurately? Can you provide guidance on how to implement a solution that ensures accurate step count data for users wearing multiple devices? We appreciate your support in resolving this matter to enhance the accuracy of our application and improve user experience.
0
0
43
16h