简体   繁体   中英

Send HTTP Requests in Background Mode (iOS) Periodically

I'm developing a great feature for my app which requires HTTP requests to be sent periodically in background.

I have read already many articles and discussions here, but haven't come up with a solution because all suggestions I met on stackoverflow.com solve the problem only partially.

Let me explain in details what do I want my application to do.

Consider there is a website with API and I want to send requests (when app is in background mode) periodically to check the state of data on the website. And if the state of data is acceptable I would send Push Notification to user to notify him that data is correct at the moment.

Could someone propose a solution how to implement this idea in iOS app?

On iOS you can't do this, as you've described it. You don't get to schedule tasks to happen at regular intervals when your app is in the background or not running. You also don't get to control whether iOS decides to terminate your app when it's running in the background, so "not running" is a case you'd need to handle (you can't just keep running in the background as for long as you want).

I'm not sure I understand the server side of things though. If your server is manipulating the data until it's acceptable, and it can send push notifications, why does it need to wait for an incoming request from the phone? Why not just send the push when the data is ready? If the app decides what's "acceptable", maybe have the app tell the server what it wants so that the server knows when to send a push.

There are a couple of options that would get close to what you describe. If you implement the "background fetch" feature in your app, iOS will launch the app when it's not running and let it make network calls in the background. There's no guarantee of how often this happens, though. This is described in Apple's background execution docs

The other option is the "silent" push notification. If your server sends one of these, iOS can launch the app in the background to handle the notification. The app could make a network call if necessary. You can send these at whatever time you like, but Apple warns to not overdo it:

Silent notifications are not meant as a way to keep your app awake in the background, nor are they meant for high priority updates. APNs treats silent notifications as low priority and may throttle their delivery altogether if the total number becomes excessive. The actual limits are dynamic and can change based on conditions, but try not to send more than a few notifications per hour.

Silent pushes are described in Apple's push notification docs .

If your app can have background-running capability then you can do a couple of things.

  • To run periodically you will need a Timer (NSTimer).
  • You will need a method which will execute the HTTP request.

For Example:

Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(handleHTTPRequest), userInfo: nil, repeats: true)

func handleHTTPRequest() {
  // fetch and change the state of data
}

Another approach would be to have delegates which will do the task when somethings happens. You can use this when you don't want to execute on fix time intervals.

iOS Background Execution Limits

Questions

How do I keep my app running continuously in the background?

If I schedule a timer, how do I get it to fire when the screen is locked?

How do I run code in the background every 15 minutes?

How do I set up a network server that runs in the background?

How can my app provide an IPC service to another one of my app while it's in the background?

Answer from Apple:

The short answer to all of these is You can't. iOS puts strict limits on background execution. Its default behavior is to suspend your app shortly after the user has moved it to the background; this suspension prevents the process from running any code.

Official: https://developer.apple.com/forums/thread/685525

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM