Pause resume / Download, even if the application terminates, is this possible?

I want to upload and download large files to or from a server from an iOS application under the following conditions.

1) If my application goes into the background, it should download and download it.

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

3) If I close the application from the tray, it will follow it at the second point when the application restarts.

I really want to know if this is possible with NSURLConnection ?

I am very familiar with the ASIHttpRequest and AFNetworking libraries, I do not want to use any external libraries, instead I want to use the default NSURLConnection for the same.

I can download and upload using NSURLConnection if its connection is permanent and the application is running in the background.

I thought of the third case:

If the application is terminated, then I will need to download or download the progress somewhere, so next time I need to send "Range" to continue downloading.

But I'm not sure how it will handle the server itself? How to configure the server for this? How to set up a web service for this? Any ideas?

+4
source share
1 answer

Since iOS5 , you can request additional time from your application for such cases. This is usually about 10 minutes, but this can be changed by the needs of the OS. To implement this in an appDelegate willResignActive application:

 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 support iOS7 , now you can use NSURLSession instead of NSURLConnection . NSURLSession can manage a full stop and resume, as well as manage a long boot using the application in the background, make smart decisions based on the wireless / cellular activity of the device so as not to drain the user battery (which is known as throttling).
You can find more information 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 at UIBackgroundTaskIdentifier on - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/

0
source

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


All Articles