Search code examples
swiftwatchkitapple-watchwatchos

WatchKit: How to trigger active background app like Music App on Watch other than HKWorkoutSession?


I read about HKWorkoutSession and they said that if I'm using HKWorkoutSession without using it for recording a workout app, it will be rejected on the App Store.

So what else is there that can keep running the app in background activity? Examples like Music App, Workout App, and Nike Running Club, if start they will have the app logo on the above screen like notification badges.

Also, if the user press the digital crown or open another app, it will automatically reopen after a few minutes of inactivity while the session is still running.


Solution

  • There is a very short list of reasons your app is allowed in the background.

    From the documentation:

    • Use an HKWorkoutSession object to start and stop workouts.

    • Use the AVAudioSession class to play extended audio files in the background.

    • Use a CLLocationManager object to start a continuous background location session.

    If your app doesn't do any of those things then it's not allowed to function in the background for more than a few seconds.

    An alternative is using Background Tasks, which allows your app to execute some tasks either on a schedule or as a response to connectivity updates. Background Tasks, however, doesn't mean that your app can remain open like with background sessions.