IOS Timer in background

I have a timer I use as a clock in my app. In the simulator it stays running even when another app is run over the top but on the actual iPhone the timer pauses until the app is the active app.

Is there a way to keep your timers running in your app if users put the app into the background?
or do I need a different approach?

If you are just wanting to measure elapsed time, store the time when put in background and compute difference when in foreground again.

This is a battery-saving “feature” of iOS – apps are suspended while in background with a couple of specific exceptions like media players (so you can keep pumping out audio, location services (so navigation can track where you are), and not much else. The exact list escapes my memory.

As I recall, a plist entry is required to request background execution, and I believe the App Store may have restrictions on what is allowed to request it.

In the case of a clock, I believe the appropriate behavior would be to just paint the current time in the event as opposed to incrementing something in your code. (If the clock is a stopwatch type thing, store the time it started and compute elapsed time when updating the UI. Don’t increment in the timer.)

1 Like

I’m having trouble getting the syntax right. I’ve copied some code from the examples in Xojo 2019 R3.2 but it says can’t find type DateTime
Activate Event
dim d1 as new datetime
sec1 = d1.SecondsFrom1970

if Sec1 > Sec2 then
var timelapse as double = sec1 - sec2
end if

Deactivate Event
dim d2 as new datetime
sec2 = d2.SecondsFrom1970

Also what event is called when an app is put into the background… Deactivate doesn’t fire

This occurs when:

  • the View is closed by calling the Close method or the user tapping the Back button
  • another View is opened (pushed) onto this view

I believe that DateTime is available in desktop projects starting with 2019R2 but not iOS projects. I think that in 2019R3’s iOS projects you must use Xojo.Core.Date instead.

dim d1 as Date = Xojo.Core.Date.Now
dim sec1 as double = d1.SecondsFrom1970

Thanks that works now but I’m still no closer to discovering the best place to put it.
No events on the visible view fire when the app is put into the background or foreground??

To tell you the truth, I have done very little with Xojo iOS (because most of my iOS projects pre-date Xojo support, and I have little incentive to move them).

I would have thought there would be events on the App object, but don’t see them listed in 2019 R2. Perhaps people can share workarounds. I do see them listed in 2020r2 under the newer replacement MobileApplication.Activated or MobileApplication.Deactivated

That said, I am not convinced you event need them for your scenario. If you are just drawing the new clock / stopwatch times in the action code of a timer, just do it there and let iOS suspend / resume your app. Your action code just will not fire while in the background. In your action code, simply draw the current time or the elapsed time from the start of what you are measuring.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.