App Timer in the background

Hi All,


I have an App that starts a Timer, this timer could potentially be put into the Background or a User might go to another App while the App Timer has begun. What I am finding with the latest OS is that my timer now goes to sleep with the app when it's put into the Background.


How can I have the Timer begin and finish even though the App is in the Background? What happening is the User starts a 1 min timer, this timer runs and the App could go into the background because the User forgot about the Timer was running. And when the App is in the background we're finding that our alarm that happens (10 seconds before the actualy timer finishes) isn't triggering and the countdown in the App stops. Is there anything I can do to work around this issue? The timer is max 5 mins. Min: 1 min.


How can I have it so that my timer in my app will finish; even if it's in the background and the alarm still goes off and can be heard by the User?


Kyle

The system will typically suspend your app shortly after the user moves it to the background [1] [2]. At that point timers start to behave oddly [3]. There’s typically two ways to deal with this:

  • If you don’t need to notify the user when the timer expires, you can cancel the timer as you go into the background and then set it back up again when you come back to the foreground.

  • If you do need to notify the user, you should use the UserNotifications framework to schedule a notification for the alarm time. If your app is in the foreground, you can catch this notification and display your own custom UI. If your app is in background, the system will display the notification for you.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[1] Unless you have some specific reason to continue running in the background, for example, you’re an audio playback app.

[2] Oh, and this doesn’t happen if you’re attached with the Xcode debugger, so you need to test this stuff by launching your app from the Home screen.

[3] There’s a bunch of complexity here based on exactly how the timer is scheduled and exactly what happens once you’re in the background.

Is there anything else we can do like leveraging the Location Services we ask for?

could we in theory have my timer running, run a check to see if the timer is going and prevent the app from going to sleep?

You have two basic paths to a solution here:

  • You can accept that your app will be suspended, and possibly terminated, while it’s in the background (A).

  • You can try to keep your app running in the background (B).

IMO, option A is the best option because:

  • Option B requires you to use a background execution mode that will let you run continually in the background. Such modes exist — for example, the audio background moded I referenced in my previous post — but they always have significant technical limitations.

  • Even if you get past those technical limitations, you still have to worry about App Review. They are very up front about the specific scrutiny that they apply to apps using these background modes (see clause 2.5.4 of the [App Store Review Guidelines][ASRG].

  • Once your timer fires, you then have to figure out how to play sound from the background, which can be a challenge.

  • Even if you manage to get this working, you still end up with an app that’s sitting in the background consuming memory and power without any great user benefit.

You can spend your time trying to get around these issues if you like, but you’d be better off using that time to dig into option A.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
App Timer in the background
 
 
Q