Prevent watch from switching back into app that has a workout session

I'm running WatchOS version 3.1 and my watch app creates a workout session, which seems to work well - my watch app code can continue to collect sensor data even when I switch to another app or watchface or the display shuts off.


The issue I'm having is that, if the user taps on the crown to switch to another app or watchface, that new app/watchface does not "stick". After the display shuts off and you wait about a minute or so and then rotate to wake up the watch, you end up back in my app again.


I understand that some apps desire this behavior, but in my case, this is not the desired behavior because I just want to silently collect sensor data in the background without impacting the user interface of the watch or the current app/watchface that the user is viewing.


Is there a way to change this behavior and insure that if the user taps on the crown to switch to another app that that new app selection "sticks"?

Try this (assuming your workout session is called hKSession):


Note - you can't set it to nil before it executes the 'didChangeState')


-(void)workoutSession:(HKWorkoutSession *)workoutSession didChangeToState:(HKWorkoutSessionState)toState fromState:(HKWorkoutSessionState)fromState date:(NSDate *)date{
    NSLog(@"workoutsession state changed to state %ld",(long)toState);
    if(toState==3){
        workoutSession=nil;
        hKSession=nil;
    }
}


- (void)didDeactivate {
    if(hKSession!=nil)
            [healthStore endWorkoutSession:hKSession];
    [super didDeactivate];
  
    /
  
}

Thanks for the tip, but unfortunately it didn't help 😟. I put in that logic you suggested to set my HKWorkoutSession reference to nil after didChangeState is called, but it didn't seem to have any impact on the app switching behavior. If I switch into a watchface and wait about a minute or so after the display shuts off, my app still ends up back in the foreground again when I rotate the watch to turn it on again.

Are you certain that you are doing a

[healthStore endWorkoutSession:hKSession];

Do you get a call to didChangeState with a value of 3?

Wait!

You are not doing that. You are maintaining the work out session as active in order to collect the data in the background. I think that means you are stuck with the behaviour you are seeing.

Right, I am collecting data in the background, and what I'm seeing seems to be the default behavior. I was hoping there is some way to modify it for apps like mine that just want to collect data in the background without impacting what app shows in the foreground.

I had a similar problem but decided I could shut down collecting data after a 20 second delay. I tried everything but nothing worked. One thing I didn't try was not declaring "Required background modes - Workout Processing" in the info.plist for the WatchKit App. I think that is what allows you to operate in the background but causes your app to reappear when the Watch is woken. Perhaps you can declare a different reason for operating in the background (like pretend to be playing audio) that will not cause your app to reappear. Good luck!

That was worth trying too, but didn't help unfortunately. I tried turning off the "Workout Processing" background mode and turning on just the Audio one and my app stopped getting sensor callbacks after a little while.


Thanks for the tips. I am going to submit this to Apple as an enhancement request and see what happens.

Prevent watch from switching back into app that has a workout session
 
 
Q