2

I have a VPN-Client app that asks the user to pause the connection for 5, 30, and 120 Minutes. The user probably leaves the application to do some work outside of my app in this period, therefore I need to Reconnect the app even if is in the background.

I tested these ways:

  • UIApplication.shared.beginBackgroundTask: It only leaves the app unsuspended for 30 seconds.
  • DispatchQueue.global(qos: .background).asyncAfter: It waits to app enter the foreground to toggle.
  • Thread.sleep in Background Thread: This waits to launch as DispatchQueue
  • Local Notification: Unfortunately it does not support silent mode as APNS.

My problem with possible ways:

  • Using APNS and Scheduled Push Notification` to send a silent message: This way probably works, but I prefer to handle it without a server.
  • Using Background Fetch from Capabilities in Background Modes: I searched a lot about this, and I think it was used for background app refresh and cycling tasks that should be run every day, hour, etc. Therefore, my case can't be used, or it's not efficient at all.
  • Using Background processing from Capabilities in Background Modes: I searched a lot about this too, I didn't quite catch that it can be used once, or this should be used in a cyclic way as Background Fetch. and my task to reconnect is not that heavy and long to use this strict feature that many times apple mentioned using alternative ways if possible.
Mahdi Moqadasi
  • 2,029
  • 4
  • 26
  • 52

1 Answers1

1

Apple only allows a very limited set of app types to run in the background:

Music streaming apps, turn-by-turn navigation apps, VoIP apps, and maybe one or 2 more. (I haven't looked at this in detail for a couple of years so my info is a little stale.)

They do support various tasks like background downloading that the system performs on your app's behalf, but you want your app to re-launch after the designated period and start running again. (Even if the user just locks their phone while your app is paused, the app won't get any CPU time and may be terminated without warning.)

In short, I suspect you are out of luck.

It seems like a VPN app is another class of app that should get "always running in the background" status.

If you are a licensed Apple developer I suggest using one of your pre-paid support tickets to ask about OS support for what you are trying to do, but I have a feeling the answer is going to be "no dice."

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • When I'm using `UIApplication.shared.beginBackgroundTask `, I can reconnect without opening the application, but the problem is iOS won't give time more than 30 seconds. I don't need an application launch. A VPN app named SharkVPN exactly has this feature! – Mahdi Moqadasi Jan 29 '23 at 18:06
  • Yup. I should have mentioned `beginBackgroundTask()` for completeness, but it’s a short duration one-shot and not useful for your situation. – Duncan C Jan 29 '23 at 21:47