2

Using Swift5.2, iOS13.4,

I try to run code in the background (when app is fully closed) at a particular date.

Local Notifications work great if App is foreground (or if user-interaction then also in background).

Some people say (here or here, that you will have to use "background timer" or "Packet Tunnel VPN" tricks in order to keep your app alive in background and in order to have a chance to run some callback-method eventually.

But is there no easier way ?

My goal is to execute code at a particular date in the future (fully from background when app is closed). How would you do that with Swift5 and >iOS12 ?

iKK
  • 6,394
  • 10
  • 58
  • 131
  • https://www.hackingwithswift.com/example-code/system/how-to-run-code-when-your-app-is-terminated – Ahmadreza Jul 12 '20 at 10:34
  • Thanks Alfi, however your link only shows an example to get an appropriate future date (and the OS decides when exactly the execution will take place). If you want to be on a particular firedate (precise!), then the only way I've found is to use the old UILocalNotification API from the iOS9 days... – iKK Jul 14 '20 at 12:00
  • @iKK did you find the solution? – A.s.ALI Dec 09 '20 at 05:59

1 Answers1

0

Short answer, there is no easy way to do this. You can use Background Fetch to perform some actions if the app is running in the background (not fully closed). The easiest way I’ve found to perform actions when the app is fully closed is connecting to Google Firebase and using cloud functions (but those are written in JavaScript? I believe and it’s fairly complex).

nicksarno
  • 3,850
  • 1
  • 13
  • 33
  • So with Google Firebase it is possible to cause an action in a fully closed App ? If yes, how ? – iKK May 15 '20 at 08:48
  • As I mentioned above, you can definitely do it. It’s just not within your Xcode project and isn’t as easy to implement as you are hoping. I had the same problem last year and came to this unfortunate conclusion. – nicksarno May 15 '20 at 13:20
  • The difference is, when you connect to Google Firebase and use Cloud Functions, they are operating by themselves and over the internet/separate from the code in your app. Whereas the code within your Xcode project only operates when the app is open. – nicksarno May 15 '20 at 13:22