Post

Replies

Boosts

Views

Activity

How to access workouts from HealthKit on WatchOS?
Hi there, I'm trying to read workouts from a WatchOS App. Unfortunately, the returned workouts are always empty. This is how it's currently implemented: func fetchWorkouts(limit: WorkoutLimit) async throws -> [HKWorkout] { let activityType = PulseConfiguration.activityType // 1. Get all workouts with the configured activity type. let walkingPredictate = HKQuery.predicateForWorkouts(with: activityType) // 2. Get all workouts that only came from this app. let sourcePredicate = HKQuery.predicateForObjects(from: .default()) // 3. Combine the predicates into a single predicate. let compound = NSCompoundPredicate(andPredicateWithSubpredicates: [walkingPredictate, sourcePredicate]) let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false) typealias WorkoutsContinuation = CheckedContinuation<[HKWorkout], Error> return try await withCheckedThrowingContinuation { (continuation: WorkoutsContinuation) in let query = HKSampleQuery( sampleType: .workoutType(), predicate: compound, limit: limit.count, sortDescriptors: [sortDescriptor] ) { _, samples, error in guard let samples = samples as? [HKWorkout], error == nil else { if let error = error { continuation.resume(throwing: error) } return } continuation.resume(returning: samples) } healthStore.execute(query) } } I noticed that the HKSource returned by .default (WatchOS) is different than the source associated to the saved workouts (Which contains the bundle identifier from the iPhone App). Is there some other way to access the workouts from the WatchOS App? Thanks in advance. Josh.
1
0
1.2k
Jun ’21
How to restore HKWorkoutSession after App relaunch?
I'm developing a running App for WatchOS. I implemented workout restoration as described here: https://developer.apple.com/documentation/watchkit/wkextensiondelegate/2976310-handleactiveworkoutrecovery. Unfortunately, it only works when a crash occurs. When the user closes the App during a workout, the session still remains and there's no way to end it when the user relaunches the App. Is there any way to retrieve the active session and restore the session like HKHealthStore().recoverActiveWorkoutSession?
1
0
872
Oct ’21
How to properly ask for location permissions in WatchOS 8
Hi. I'm developing a running App for WatchOS. During onboarding I ask for location permission in order to save the workout route. I use requestWhenInUseAuthorization()on a CLLocationManager. Since WatchOS 8, I have encountered some weird behavior. After a while the permission seems to "reset" without any interaction by the user or the App. When I check the permission in WatchOS, its state is "Ask or When I Share". Am I'm missing something or is it a bug? I'm also unable to change the permissions in settings.
1
0
1k
Dec ’21
How to get Auto-Pause preference from the watchOS Settings?
My App with outdoor-running activity type receives HKWorkoutEvent.motionPaused und HKWorkoutEvent.motionResumed events from HKLiveWorkoutBuilderDelegate, even if the User toggled the feature off in the workout settings (Settings -> Workout -> Auto-Pause). I guess this is unexpected behavior (FB11754136), but is there a way to check the user preference from the watchOS settings? Kind regards, Josh
1
0
1.5k
Nov ’22