Continue Aborted Download on iPhone

I use NSURLConnection to download large files from the Internet to the iPhone. I use the didReceiveData method to add data to a file in the Documents folder. It works great.

If the download is interrupted (for example, because the user clicked the "home" button), I would like to be able to continue downloading the next time I start the application, and not from scratch!

Can anybody help me?

+1
source share
3 answers

ASIHTTPRequest has easy-to-use support for resuming downloads:

http://allseeing-i.com/ASIHTTPRequest/How-to-use#resume

Alternatively, find out how much data you have already downloaded by looking at the size of the existing data and set the Range header to NSMutableURLRequest :

[request addValue:@"bytes=x-" forHTTPHeaderField:@"Range"]; 

.. where x is the size in bytes of data that you already have. This will load data starting at byte x. You can then add this data to your file as it is received.

Note that the web server you are loading must support partial loading for the resource you are trying to load - if it sends an Accept-Ranges header. Normally, you cannot resume loading dynamically generated content (for example, the page generated by a script on the server).

+7
source

The only method I can think of is to split the file that you upload into smaller files. you can use

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 

to determine when each part of the file ended, and reload the download in the last part that did not load.

-1
source

Look at the HTTP standard to learn how to form a request to start transferring a resource from a specific offset. Sorry, I can not find any code.

-1
source

Source: https://habr.com/ru/post/1443796/


All Articles