-1

I want my iOS Swift app to run in the background. The things I want to run in the background are:

  • Timer - that pings server periodically and retrieves data from it and then its displayed on the tableView.
  • Audio - If a certain condition is encountered in the retrieved data, I play a local audio clip from the app via the avf framework.

The app is working as expected. I have done the following to allow backgrounding:

enter image description here

It does work in the background. However, is there any additional thing that I need to do? such that the app doesn't get rejected from the Apple App store.

Abizern
  • 146,289
  • 39
  • 203
  • 257
deathAdder
  • 13
  • 8
  • https://www.raywenderlich.com/5817-background-modes-tutorial-getting-started – lorem ipsum Jul 17 '21 at 13:25
  • Have you tested your app when not connected to Xcode. While it is probably working for you under Xcode it will be killed if you run it normally. You get unlimited background execution under Xcode but only a couple of minutes normally. You can get around this by continually playing audio silence but it will interfere with other audio non the device and is a hack that won't be accepted to the App Store. As Gnasher says this is exactly why push notifications are a thing. – Paulw11 Jul 17 '21 at 21:37
  • @Paulw11 I tested on a real device and yes iOS did kill it. However what I am looking for is the app to keep running in the background, similar to how in android a user can add an app to 'protected apps' which means that android wont kill them, and they can run and do their work. I was able to make it run for a short while via UIApplication.shared.beginBackgroundTask and coding accordingly but it isn't idea. The clients want the app to run continuously as battery isnt of concern for them. I dont think push notifs can work here as data needs to be read in xml which can change w/o discretion – deathAdder Jul 19 '21 at 10:55
  • You just won't do it on iOS unless you keep the app open on the foreground.. You should have a server that is monitoring the data and when it detects a change it sends a push notification to the app. – Paulw11 Jul 19 '21 at 12:03
  • @Paulw11 Right so iOS doesn't have the same bg capabilities as android then. Can the server checking the data be implemented on like firebase? – deathAdder Jul 19 '21 at 21:02
  • Yes, you could use a cloud function to poll for changes. https://cloud.google.com/scheduler/docs/tut-pub-sub – Paulw11 Jul 19 '21 at 21:34

1 Answers1

2

“Timer That pings the server periodically” - that will make Apple hate you. And if Apple allowed it on the App Store, users whose battery you are emptying will hate you.

Read up on push notifications. No pinging, no energy use at all. Doesn’t even use WiFi or mobile data - your phone service provider sends you the push at a level below mobile data.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Let me elaborate on the issue, the app and the users which this is being made for want this functionality. As the app is intended to be used with a product in law enforcement, so emptying battery is not of concern for that use case. Also the server does not send push notifications, hence I cant use that. The app needs to **listen** for the data in xml from the server and sound the alarm if necessary. So how would this be achieved? do I need to put in additional code? or is the checking in the app info enough? – deathAdder Jul 17 '21 at 15:25
  • @gnasher, you are mistaken with how push notifications work. It is merely a maintained TCP connection from the device to Apple's servers. It can use a cellular or wifi connection. https://support.apple.com/en-au/guide/deployment-reference-ios/ior9d28751c0/web If it were any different how would it work on non-cellular iPads or iPods etc. You are right that it does use very little data and energy however since it merely needs to send the occasional keep-alive and iOS can do this when the radio is active for other reasons. – Paulw11 Jul 17 '21 at 21:34
  • False: > your phone service provider sends you the push at a level below mobile data – Soheil Sep 17 '22 at 02:56