Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Background downloads when the app is not active

Tags:

When my app is initially downloaded, the user needs to download a large file something like 200MB (upper limit). I definitely can't expect the user to keep the app open till this file is downloaded. So he/she might close the app & the app will go into background.

How can I continue to download the file in this scenario? Is this even possible in iOS?

like image 308
Srikar Appalaraju Avatar asked Jan 14 '12 09:01

Srikar Appalaraju


People also ask

Can iOS apps download in background?

iOS will not allow an app to run in the background unless the developer has coded specifically to request the operating system to allow it. Without the developer writing code to keep the app "alive" and downloading, there's nothing you can do.

How do I stop background downloads on iPhone?

How to turn off Background App Refresh on your iPhone and iPad. Go to Settings > General > Background App Refresh. Tap Background App Refresh > Off to turn Background App Refresh off completely.

How do you see if anything is downloading in the background on iPhone?

All replies. Check in Settings>iTunes & App Store>Automatic Downloads>Updates>On. If that is set to "On", any apps that require updates are being downloaded in the background.


2 Answers

Add below in your - (void)applicationDidEnterBackground:(UIApplication *)application

UIApplication  *app = [UIApplication sharedApplication]; UIBackgroundTaskIdentifier bgTask;  bgTask = [app beginBackgroundTaskWithExpirationHandler:^{          [app endBackgroundTask:bgTask];  }]; 

and you are good to go... I have this in one of my published download manager app

This will work just fine. You can also check how much time you have, since apple only enable 10 minutes background tasks. Use:

NSTimeInterval ti = [[UIApplication sharedApplication]backgroundTimeRemaining]; NSLog(@"backgroundTimeRemaining: %f", ti); // just for debug 
like image 93
Saurabh Avatar answered Dec 25 '22 05:12

Saurabh


It is possible to start a background task when you begin the download:

Apps that are transitioning to the background can request an extra amount of time to finish any important last-minute tasks.

Executing a Finite-Length Task in the Background

However, such a task is limited to an undefined amount of execution time by the system. However, a 200Mb file download may be too large a task in this case.

like image 45
Chris Gummer Avatar answered Dec 25 '22 06:12

Chris Gummer