URLSessionDownloadTask
A URL session task that stores downloaded data to a file.
Declaration
class URLSessionDownloadTaskMentioned in
Overview
An URLSessionDownloadTask is a concrete subclass of URLSessionTask, which provides most of the methods for this class.
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 in the suspended state or 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.
Download delegate behavior
When you use a download task, your delegate receives several callbacks unique to download scenarios.
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:didFinishDownloadingTo:) 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. The only errors your delegate receives through the
errorparameter are client-side errors, such as being unable to resolve the hostname or connect to the host. To check for server-side errors, inspect the response property of thetaskparameter received by this callback.