Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Finishing a task when entering background

If I have a large file download going on an the app gets moved to background, is there any way to keep the download executing functions alive?

I knowbeginBackgroundTaskWithExpirationHandler: gets called when the app moves to the background and I can start my task there, but I don't want to start a new task, I want to complete my old task. It can be solved with beginBackgroundTaskWithExpirationHandler:, but for that I need to pause my download and resume it from the right place, which is just plain silly.

Ideally what I want is that I wrap my download function with an expiration handler, so my download function keeps executing for the permitted time after the app has been moved to the background.

like image 501
SiimKallas Avatar asked Sep 04 '12 10:09

SiimKallas


People also ask

How do you stop apps from closing in background iOS?

Please check the following settings to prevent this. – Please make sure Background App Refresh is enabled for the app. You can find it under device Settings -> General -> Background App Refresh switch. Both Wi-Fi & Cellular Data should be on and our app should be toggled on for permission.

How do you run an application continuously running in the background Swift?

First, go to your project's settings and choose the Capabilities tab. You need to enable the Background Modes capability, then check the box marked Background Fetch. This modifies your app's Info. plist file to add the “fetch” background mode that enables all the following functionality.

What long running tasks can iOS support in the background?

The answer is simply 600 seconds (10 minutes), reason is provided by the article above.

What happens when app goes background iOS?

It will be resumed when the application is brought back to the foreground. However, in the latter case you should still be prepared for your application to be terminated at any time while it's in the background by cleaning things up on your way to the background.


1 Answers

Ideally what I want is that I wrap my download function with an expiration handler, so my download function keeps executing for the permitted time after the app has been moved to the background.

This is exactly how it works. beginBackgroundTaskWithExpirationHandler: is not called when you enter the background. It's what you call to indicate that you're starting something that, if you happen to go into the background while it's running, you would like to finish. Just wrap your existing download code with beginBackgroundTaskWithExpirationHandler: and endBackgroundTask:.

like image 84
Rob Napier Avatar answered Nov 09 '22 23:11

Rob Napier