Download files in the background without stopping after 10 minutes on iOS7 / iOS8
We are working on a music app. The app has the ability to download music files even if the user hides the app, but it only works for about 10 minutes and stops . We read a lot of articles about this and found out that it is impossible to download it all the time unless you are magazines / newspapers. BUT we have many clients complaining about this .
Is there any new way to do this in iOS7 / iOS8 using NSURLSession or something?
Some applications use CoreLocation to update the current location and start downloading. but it looks like it's a hack and it's not good for us.
Edited after some comments:
Our download loop: we have a queue of tasks to download. every time we run first in queue from coredata -> get url from backend to load track -> use NSURLConnection to load -> encrypt file, save and save some info in coredata -> run next one if available.
So we don't know the download url, eg. NSURLSessionDownloadTask before actually starting due to protected restrictions
source to share
The reason your transfer breaks after 10 minutes is because iOS pauses your app after about 10 minutes in the background. Thus, this is indeed the expected behavior.
Yes, there is a way to upload files in iOS 7+ using
a background session setting object (
). In this case, the actual transfer will be done outside of the process, and your application will be notified of the result via the delegate (
) method .
This probably won't work with your stream out of the box, because you need to know the actual URL to run the download task in the background. However, you can try refactoring the server side of your application, for example. redirect the request to the actual download instead of returning the url - in which case
will work for you (and you can pass any number of custom HTTP headers if you rely on that too).
Another option is to download the download url, process it, and initiate the download of a different background. This, however, will complicate client logic significantly (IMO) and make debugging testing a little more difficult - background network transfers are not the easiest thing to get right and not easy to debug.
source to share