Download tasks directly write the server’s response data to a temporary file, providing your app with progress updates as data arrives from the server. When you use download tasks in background sessions, these downloads continue even when your app is suspended or is otherwise not running.
You can pause (cancel) download tasks and resume them later (assuming the server supports doing so). You can also resume downloads that failed because of network connectivity problems.
When you use download tasks:
During download, the session periodically calls the delegate’s
URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:method with status information.
Upon successful completion, the session calls the delegate’s
URLSession:downloadTask:didFinishDownloadingToURL:method or completion handler. In that method, you must either open the file for reading or move it to a permanent location in your app’s sandbox container directory.
Upon unsuccessful completion, the session calls the delegate’s
URLSession:task:didCompleteWithError:method or completion handler. Unlike
NSURLSessionDownloadTaskreports server-side errors reported through HTTP status codes into corresponding
Cancels a download and calls a callback with resume data for later use.
- (void)cancelByProducingResumeData:(void (^)(NSData *resumeData))
A completion handler that is called when the download has been successfully canceled.
If the download is resumable, the completion handler is provided with a
resumeDataobject. Your app can later pass this object to a session’s
downloadTaskWithResumeData:completionHandler:method to create a new task that resumes the download where it left off.
This block is not guaranteed to execute in a particular thread context. As such, you may want specify an appropriate dispatch queue in which to perform any work.
A download can be resumed only if the following conditions are met:
The resource has not changed since you first requested it
The task is an HTTP or HTTPS
The server provides either the
Last-Modifiedheader (or both) in its response
The server supports byte-range requests
The temporary file hasn’t been deleted by the system in response to disk space pressure
Available in iOS 7.0 and later.