UIBackgroundTask killing app on completion despite Audio background mode

My app uses the Media Player framework to play music and has the Audio background mode. When a user pauses the music and backgrounds the app (or if the app is already in the background when they pause), the system does not kill the app as long as it has the active AVAudioSession. This means that as long as a user doesn't start playback from another audio app, mine is available in Control Center for a quick resume.

I recently implemented the UIBackgroundTask API (specifically UIApplication.shared.beginBackgroundTask/endBackgroundTask) to run a 5-20 second server communication task when the app is paused while in the background. Now, iOS kills my app at the conclusion of that task, despite it still having the active audio session. It is no longer in Control Center, and needs to be launched fresh.

Is there anything I can do to prevent the system from killing my app at the conclusion of the background task? I'm starting to get complaints from users that they're having to relaunch the app after every time they pause for more than a few seconds.

Thanks!

Accepted Reply

Are you absolutely sure that you end every background task that you start? It’s easy to get that wrong, and it’s common cause of problems like this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • Thanks for the response! I am pretty sure that I am, but I'll go back through everything with a fine toothed comb. Just to confirm, your understanding is that if I'm ending the background task correctly and in the allotted time, my app shouldn't be killed at the conclusion and will remain the Now Playing app?

  • Aha! This was it after all. For whatever reason, multiple "MPMusicPlayerControllerPlaybackStateDidChange" notifications were being sent on pause, which meant that the backup job was being kicked off multiple times, starting a new background task each time. Since the task was in progress, a later stage didn't run, which meant the additional tasks were expiring. Now that that's all cleaned up the app indeed stays as the now playing app after the task ends. Many, many thanks, @eskimo!

Add a Comment

Replies

Are you absolutely sure that you end every background task that you start? It’s easy to get that wrong, and it’s common cause of problems like this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

  • Thanks for the response! I am pretty sure that I am, but I'll go back through everything with a fine toothed comb. Just to confirm, your understanding is that if I'm ending the background task correctly and in the allotted time, my app shouldn't be killed at the conclusion and will remain the Now Playing app?

  • Aha! This was it after all. For whatever reason, multiple "MPMusicPlayerControllerPlaybackStateDidChange" notifications were being sent on pause, which meant that the backup job was being kicked off multiple times, starting a new background task each time. Since the task was in progress, a later stage didn't run, which meant the additional tasks were expiring. Now that that's all cleaned up the app indeed stays as the now playing app after the task ends. Many, many thanks, @eskimo!

Add a Comment