5

I want to upload and download big files to or from the server from an iOS app in the following conditions.

1) If my app goes into the background it should perform its uploading and downloading.

2) If I lost the connection then it should stop any progressive upload and download, and then can resume back from where it stopped.

3) If I close the app from the tray then it will follow the same in 2nd point when app restarts.

I really want to know is this possible with NSURLConnection?

I am very well aware of ASIHttpRequest and AFNetworking libraries, I don't want to use any external libraries instead want to use default NSURLConnection class for the same.

I am able to download and upload with NSURLConnection if its connection is persistent and application running in the background.

I've the following thought in my mind for the 3rd case:

If app will going to terminate then, I'll need to store uploading or downloading progress somewhere so that next time I'll need to send "Range" for the continue download.

But I am not sure how it will handle by server it self? How to configure the server for this? How to configure webservice for this? Any ideas?

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • You should look into `NSURLCacheStoragePolicy`, and by the way any external library uses `NSURLConnection` only, so you can opt to use them – iphonic Aug 17 '13 at 06:19
  • This is a really difficult question to accurately answer, as much of the answer depends on what kind of server you're doing the file transfer with, if it supports pausing / restarting the file transfer, if you have control over the server, etc. – Michael Dautermann Aug 17 '13 at 06:39
  • @MichaelDautermann, Thanks for your comment Michael! Do you've any reference source where I can get to know how to configure my server for this? – Hemang Aug 17 '13 at 12:38
  • I'm afraid that allowing user to resume his download is totally different thing that allowing to resume upload, and to answer this one would need to write 2 answers, really. One (user downloading) pretty easy, second (user uploading) pretty complicated. – Mołot Oct 15 '13 at 19:55
  • @Mołot, thanks for your comment, I might ask two separate questions. Anyways I have written it out here that someone will easily get the solution if this will get an answer. – Hemang Oct 16 '13 at 05:23

1 Answers1

1

Since iOS5 you can request your application some extra time for this kind of occasions. It is usually around 10 minutes, but it could be changed by the OS needs. In order to implement this in your appDelegate willResignActive:

UIBackgroundTaskIdentifier backGroundTask = 0;
backGroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
    //time span give expired and the download is not finished
}];


//once the download is finished inside the time span given
[[UIApplication sharedApplication] endBackgroundTask:backGroundTask];

If you are supporting iOS7 only you can now make use of NSURLSession instead of NSURLConnection. NSURLSession can manage a complete stop and resume as well as manage a long download with the app in the background, making sensible decisions based on the wireless/cell activity of the device in order of not drain the user battery (what is know as throttling).
You can find more about NSURLSession on - https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/Introduction/Introduction.html
Another interesting article by Matt Thompson can be found here - http://www.objc.io/issue-5/from-nsurlconnection-to-nsurlsession.html
Also find more on UIBackgroundTaskIdentifier on - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/

cescofry
  • 3,706
  • 3
  • 26
  • 22