DownloadRequest

open class DownloadRequest : Request

Specific type of Request that manages an underlying URLSessionDownloadTask.

Helper Types

  • A collection of options to be executed prior to moving a downloaded file from the temporary URL to the destination URL.

    See more

    Declaration

    Swift

    public struct DownloadOptions : OptionSet
  • A closure executed once a download request has successfully completed in order to determine where to move the temporary file written to during the download process. The closure takes two arguments: the temporary file URL and the URL response, and returns a two arguments: the file URL where the temporary file should be moved and the options defining how the file should be moved.

    Declaration

    Swift

    public typealias DownloadFileDestination = (
    	_ temporaryURL: URL,
    	_ response: HTTPURLResponse)
    -> (destinationURL: URL, options: DownloadOptions)

Properties

  • The request sent or to be sent to the server.

    Declaration

    Swift

    open override var request: URLRequest? { get }
  • The resume data of the underlying download task if available after a failure.

    Declaration

    Swift

    open var resumeData: Data? { get }
  • The progress of downloading the response data from the server for the request.

    Declaration

    Swift

    open var progress: Progress { get }

State

  • Cancels the request.

    Declaration

    Swift

    override open func cancel()
  • Cancels the request.

    Declaration

    Swift

    open func cancel(createResumeData: Bool)

    Parameters

    createResumeData

    Determines whether resume data is created via the underlying download task or not.

Progress

  • Sets a closure to be called periodically during the lifecycle of the Request as data is read from the server.

    Declaration

    Swift

    @discardableResult
    open func downloadProgress(queue: DispatchQueue = DispatchQueue.main, closure: @escaping ProgressHandler) -> Self

    Parameters

    queue

    The dispatch queue to execute the closure on.

    closure

    The code to be executed periodically as data is read from the server.

    Return Value

    The request.

Destination

  • Creates a download file destination closure which uses the default file manager to move the temporary file to a file URL in the first available directory with the specified search path directory and search path domain mask.

    Declaration

    Swift

    open class func suggestedDownloadDestination(
    	for directory: FileManager.SearchPathDirectory = .documentDirectory,
    	in domain: FileManager.SearchPathDomainMask = .userDomainMask)
    -> DownloadFileDestination

    Parameters

    directory

    The search path directory. .DocumentDirectory by default.

    domain

    The search path domain mask. .UserDomainMask by default.

    Return Value

    A download file destination closure.

  • Wait for the request to finish then return the response value.

    Declaration

    Swift

    public func response() -> DefaultDownloadResponse

    Return Value

    The response.

  • Wait for the request to finish then return the response value.

    Declaration

    Swift

    public func response<T>(responseSerializer: T) -> DownloadResponse<T.SerializedObject> where T : DownloadResponseSerializerProtocol

    Parameters

    responseSerializer

    The response serializer responsible for serializing the request, response, and data.

    Return Value

    The response.

  • Wait for the request to finish then return the response value.

    Declaration

    Swift

    public func responseData() -> DownloadResponse<Data>

    Return Value

    The response.

  • Wait for the request to finish then return the response value.

    Declaration

    Swift

    public func responseJSON(options: JSONSerialization.ReadingOptions = .allowFragments) -> DownloadResponse<Any>

    Parameters

    options

    The JSON serialization reading options. .AllowFragments by default.

    Return Value

    The response.

  • Wait for the request to finish then return the response value.

    Declaration

    Swift

    public func responseString(encoding: String.Encoding? = nil) -> DownloadResponse<String>

    Parameters

    encoding

    The string encoding. If nil, the string encoding will be determined from the server response, falling back to the default HTTP default character set, ISO-8859-1.

    Return Value

    The response.

  • Wait for the request to finish then return the response value.

    Declaration

    Swift

    public func responsePropertyList(options: PropertyListSerialization.ReadOptions = PropertyListSerialization.ReadOptions()) -> DownloadResponse<Any>

    Parameters

    options

    The property list reading options. Defaults to [].

    Return Value

    The response.

Default

  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func response(
    	queue: DispatchQueue? = nil,
    	completionHandler: @escaping (DefaultDownloadResponse) -> Void)
    -> Self

    Parameters

    queue

    The queue on which the completion handler is dispatched.

    completionHandler

    The code to be executed once the request has finished.

    Return Value

    The request.

  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func response<T: DownloadResponseSerializerProtocol>(
    	queue: DispatchQueue? = nil,
    	responseSerializer: T,
    	completionHandler: @escaping (DownloadResponse<T.SerializedObject>) -> Void)
    -> Self

    Parameters

    queue

    The queue on which the completion handler is dispatched.

    responseSerializer

    The response serializer responsible for serializing the request, response, and data contained in the destination url.

    completionHandler

    The code to be executed once the request has finished.

    Return Value

    The request.

Data

  • Creates a response serializer that returns the associated data as-is.

    Declaration

    Swift

    public static func dataResponseSerializer() -> DownloadResponseSerializer<Data>

    Return Value

    A data response serializer.

  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func responseData(
    	queue: DispatchQueue? = nil,
    	completionHandler: @escaping (DownloadResponse<Data>) -> Void)
    -> Self

    Parameters

    completionHandler

    The code to be executed once the request has finished.

    Return Value

    The request.

String

  • Creates a response serializer that returns a result string type initialized from the response data with the specified string encoding.

    Declaration

    Swift

    public static func stringResponseSerializer(encoding: String.Encoding? = nil) -> DownloadResponseSerializer<String>

    Parameters

    encoding

    The string encoding. If nil, the string encoding will be determined from the server response, falling back to the default HTTP default character set, ISO-8859-1.

    Return Value

    A string response serializer.

  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func responseString(
    	queue: DispatchQueue? = nil,
    	encoding: String.Encoding? = nil,
    	completionHandler: @escaping (DownloadResponse<String>) -> Void)
    -> Self

    Parameters

    encoding

    The string encoding. If nil, the string encoding will be determined from the server response, falling back to the default HTTP default character set, ISO-8859-1.

    completionHandler

    A closure to be executed once the request has finished.

    Return Value

    The request.

JSON

  • Creates a response serializer that returns a JSON object result type constructed from the response data using JSONSerialization with the specified reading options.

    Declaration

    Swift

    public static func jsonResponseSerializer(
    	options: JSONSerialization.ReadingOptions = .allowFragments)
    -> DownloadResponseSerializer<Any>

    Parameters

    options

    The JSON serialization reading options. Defaults to .allowFragments.

    Return Value

    A JSON object response serializer.

  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func responseJSON(
    	queue: DispatchQueue? = nil,
    	options: JSONSerialization.ReadingOptions = .allowFragments,
    	completionHandler: @escaping (DownloadResponse<Any>) -> Void)
    -> Self

    Parameters

    options

    The JSON serialization reading options. Defaults to .allowFragments.

    completionHandler

    A closure to be executed once the request has finished.

    Return Value

    The request.

Property List

  • Creates a response serializer that returns an object constructed from the response data using PropertyListSerialization with the specified reading options.

    Declaration

    Swift

    public static func propertyListResponseSerializer(
    	options: PropertyListSerialization.ReadOptions = [])
    -> DownloadResponseSerializer<Any>

    Parameters

    options

    The property list reading options. Defaults to [].

    Return Value

    A property list object response serializer.

  • Adds a handler to be called once the request has finished.

    Declaration

    Swift

    @discardableResult
    public func responsePropertyList(
    	queue: DispatchQueue? = nil,
    	options: PropertyListSerialization.ReadOptions = [],
    	completionHandler: @escaping (DownloadResponse<Any>) -> Void)
    -> Self

    Parameters

    options

    The property list reading options. Defaults to [].

    completionHandler

    A closure to be executed once the request has finished.

    Return Value

    The request.