Reputation: 27072
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?
Upvotes: 5
Views: 1118
Reputation: 3704
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/
Upvotes: 1