Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files in background without stopping after 10 minutes in iOS7/iOS8

We are working on the music app. The app has possibility to download music files even if the user hides the app but it works only around 10 minutes and stops. We read many articles about it, and found out that it is kind of impossible to download constantly if you are not a magazines/ newspapers. BUT we have lots of customers complains about it.

Is there any new way to do it in iOS7/iOS8 with using NSURLSession or something else?

Some apps use CoreLocation to get update of the current location and start downloading. but it feels like this is a hack and it is not good for us.

Edited after some comments:

Our Download cycle: we have a queue of tasks to download. each time we start the first in queue from the coredata -> get url from the backend to download the track -> use NSURLConnection to download -> encrypt the file, store and save some info to coredata -> start the next one if any.

So, we don´t know the url to download for e.g. NSURLSessionDownloadTask before the actual start because of secure restrictions

like image 711
kabarga Avatar asked Feb 01 '26 15:02

kabarga


1 Answers

The reason your transfer is interrupted after 10 minutes is because iOS suspends your app after about 10 minutes in background. So it is actually an expected behaviour.

Yes, there is a way to download files in iOS 7+ by using NSURLSession with background session configuration object ([NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:]). In this case actual transfer will be performed out-of-process and your app will be notified of the result via delegate method ([UIApplicationDelegate application:handleEventsForBackgroundURLSession:completionHandler:]).

This probably won't work with your flow out-of-the-box because to initiate download task in background you need to know actual URL. You can, however, try refactoring server-side part of the application to e.g. redirect request to actual download instead of returning a URL – in this case NSURLSession will work for you (and you can pass any number of custom HTTP headers if you rely on this, too).

Another option would be to download your download URL, process it, and initiate another background download. This, however, will complicate client's logic significantly (IMO) and make testing a debugging somewhat harder – background network transfers isn't the simplest thing to get right and isn't easiest thing to debug.

like image 140
Andrey Avatar answered Feb 03 '26 05:02

Andrey