Running background task on startup

2020-07-17 17:04发布

问题:

I'm writing an iOS app that uses the Dropbox Datastore API to sync data across multiple devices.

On Android, I'm able to tell the service to start on boot, which enables it to sync any changes that may have occurred whilst the device was turned off.

I'm having trouble finding a definitive way to have my app do the same on iOS.

So, does anyone have any recommendations to:

  • Run a simple background service efficiently and dependably on iOS.
  • Have the service start at boot

This must be possible - apps like Facebook and Gmail start syncing as soon as the device finishes booting.

回答1:

Run a simple background service efficiently and dependably on iOS.

You cannot run background services on iOS. You can perform some operations in background, but the OS reserves the right of killing your app at any time.

Have the service start at boot

Impossible. Third-party applications cannot run automatically at boot on iOS.

This must be possible - apps like Facebook and Gmail start syncing as soon as the device finishes booting.

Given what stated above, I definitely can tell you that they don't. It might look like, but they cannot technically do that.

One option you have on iOS 7 is to have your app to refresh data in background when a special push notification is received, as discussed here. Long story short, you can send a push notification that triggers the data refresh and it's probably what Facebook and GMail do, in order to give the impression that the content was constantly updated in background.



回答2:

When your app is launched, it could begin updating / syncing Dropbox data in the background.

It is not possible to have a service that runs when the device boots. The Facebook app and Gmail app do not have a service / process that runs when the device boots.



回答3:

Just to be complete:

There are a number of IOS background modes (UIBackgroundModes) you can setup for an application.

There is one that does allow an application to be started on startup (if your app was running on shutdown) and also to be auto-restarted when it crashes (it's always restarted in the background). That is the "Voip" background app type.

Most likely not useful to you though as apple are very strict about what a "voip" application is and is not.