HKObjectQuery Won't Stop

78 Views Asked by At

I am trying to create a nice user experience for doing workouts on my app using an Apple Watch. I can start an HKWorkoutSession on my watch and stop it fine, in the foreground and in the background.

My problem is that the HKObjectQuery does not seem to stop when the watch app is in the background. To clarify, if I lock the watch and the phone for about 3 minutes during a workout and then try to stop stop the workout from the phone app nothing happens.

Any solutions, or even an explanation as to why would be greatly appreciated.

1

There are 1 best solutions below

0
On

You need to enable background modes for your WatchKit Extension.

WatchKit Background mode for Workouts

App Programming Guide for watchOS: Leveraging iOS Technologies states:

Watch apps are considered foreground apps; they run only while the user interacts with one of their interfaces. As a result, the corresponding WatchKit extension cannot take advantage of most background execution modes to perform tasks. However, there are a few exceptions:

  • Use an HKWorkoutSession object to start and stop workouts. Use of this object requires enabling the Workout Processing background mode capability in your WatchKit extension target (not your WatchKit App target). Enabling this mode adds the UIBackgroundModes key to the extension’s Info.plist file.

Now when your app has started a workout on the Apple Watch, it will respond to WatchConnectivity messages in the background.
So, if you have handled the WatchConnectivity message to stop the workout, it will stop.