Tertium
Tertium

Reputation: 6308

Background task timers on iOS

I have an android/winphone application. Small part of app (background broadcastreceiver/task) from time to time (approx 10-15 minutes) checks local file with timestamps and if difference between one of timestamps and current time becomes more than - say - 30 minutes, it shows a notification to a user.

In Android I use AlarmManager.setRepeating(...) to setup periodic check, and to run this setup code I register a BroadcastReceiver on action "android.intent.action.BOOT_COMPLETED" in Manifest. Or call AlarmManager.cancel(...) and AlarmManager.setRepeating(...) on every launch. This way I may ber sure my timers work.

In Windows Universal I use triggers and background tasks. This works the same way with some limitations.

Now I'm porting my app to iOS, and quick look doesn't give me a way to register a function or class to perform quick checks (mostly less than a second) every 10 minutes while the app itself is not launched, and also I would like to register a function on device boot event. Is it possible? Or should I keep my app running in background? I'd prefer not to do so, as my checks are lightweight and rare.

Upvotes: 0

Views: 713

Answers (1)

Vyacheslav
Vyacheslav

Reputation: 27221

You can use background fetch for quick executions. But there are several disadvantages:

  1. Doesn't work in sleep mode.

  2. You cannot catch device-launch event.

  3. It will be dropped in 30 seconds.

I think in common case this is not analog for AlarmManager. Forget about it.

https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial

https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Push notification can be used as a trigger. But, nevertheless, this is a server-side implementation.

In the guide above:

  1. Check the box Background fetch in the Background Modes of your app’s Capabilities.

  2. Use setMinimumBackgroundFetchInterval(_:) to set a time interval appropriate for your app.

  3. Implement application(_:performFetchWithCompletionHandler:) in your app delegate to handle the background fetch.

Your callback event is performFetchWithCompletionHandler

Upvotes: 1

Related Questions