2

How can I run a callback even if the app goes into the background in iOS? I want to delay a function and run it only once after 5 to 30 minutes (time of my choice).

Is there anyway to achieve this? My use case is, when the user postpones an event for 5 minutes, after 5 minutes I would like to call a function to resume the event.

  • I tried react native background fetch and background timer but still no good
  • I want to run my function after 5 to 30 minutes (time of my choice) even if app is in the background

Edit:

  • The specific thing we're trying to achieve is to start blocking distractions via the Managed Settings API after a certain time period. The user is able to postpone blocking for say 15 minutes and we want to reactivate it after that time
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    Look into "Background tasks" and "Background Modes" background execution is very limited in iOS you need to have a good reason or rely on Apple's metered options. – lorem ipsum Aug 06 '23 at 12:42
  • @loremipsum may i ask what are the metered options? – Hồ Gia Bảo Aug 06 '23 at 12:47
  • Background Tasks – lorem ipsum Aug 06 '23 at 12:48
  • 1
    Generally the answer is no. What specific thing are you trying to achieve (what does the callback do)? For very specific use cases, there are solutions, but there is no general solution to this question. The closest are a silent push notification from your server (which has some restrictions, but generally is allowed), or a local notification that will display a message that the user may choose to launch your app with. For certain specialized problems, there other solutions. But as asked, the answer is no. – Rob Napier Aug 06 '23 at 21:53
  • 1
    @robNapier the specific thing we're trying to achieve is to start blocking distractions via the Managed Settings API after a certain time period. The user is able to postpone blocking for say 15 minutes and we want to reactivate it after that time. – jeznag Aug 06 '23 at 23:43
  • [Using background tasks to update your app](https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app) – clawesome Aug 07 '23 at 13:21
  • If there's a way to do what you're describing, it would likely be a time-based policy in ManagedSettings, though I don't see one. Generally apps cannot modify those kinds of settings. Apple provides the UI, apps just provide information and respond. Sometimes there are explicit extension points (like the Shield configuration stuff). But the one thing that almost never is the answer is "my app gets to run arbitrary code at an specific time without user intervention." That's not really a thing in iOS. Similarly, apps generally cannot influence other apps. – Rob Napier Aug 07 '23 at 13:49
  • 1
    (I'm not particularly familiar with this API, so I would watch the videos and see how Apple intends you to use it. But the solution is not going to look like the question you've asked, and I would start with the assumption that it's impossible, because Apple generally does not allow these kinds of operations.) – Rob Napier Aug 07 '23 at 13:50

0 Answers0