-
A set of options to be executed prior to moving a downloaded file from the temporary
See moreURLto the destinationURL.Declaration
Swift
public struct Options : OptionSet, Sendable
-
A closure executed once a
DownloadRequesthas 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 theHTTPURLResponse, and returns two values: the file URL where the temporary file should be moved and the options defining how the file should be moved.Note
Downloads from a localfile://URLs do not use theDestinationclosure, as those downloads do not return anHTTPURLResponse. Instead the file is merely moved within the temporary directory.Declaration
Swift
public typealias Destination = @Sendable (_ temporaryURL: URL, _ response: HTTPURLResponse) -> (destinationURL: URL, options: Options) -
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
public class func suggestedDownloadDestination(for directory: FileManager.SearchPathDirectory = .documentDirectory, in domain: FileManager.SearchPathDomainMask = .userDomainMask, options: Options = []) -> DestinationParameters
directoryThe search path directory.
.documentDirectoryby default.domainThe search path domain mask.
.userDomainMaskby default.optionsDownloadRequest.Optionsused when moving the downloaded file to its destination. None by default.Return Value
The
Destinationclosure.
-
Type describing the source used to create the underlying
See moreURLSessionDownloadTask.Declaration
Swift
public enum Downloadable
-
If the download is resumable and is eventually cancelled or fails, this value may be used to resume the download using the
download(resumingWith data:)API.Declaration
Swift
public var resumeData: Data? { get } -
If the download is successful, the
URLwhere the file was downloaded.Declaration
Swift
public var fileURL: URL? { get }
-
Downloadablevalue used for this instance.Declaration
Swift
public let downloadable: Downloadable -
Creates a
URLSessionTaskfrom the provided resume data.Declaration
Swift
public func task(forResumeData data: Data, using session: URLSession) -> URLSessionTaskParameters
dataDataused to resume the download.sessionURLSessionused to create theURLSessionTask.Return Value
The
URLSessionTaskcreated. -
Cancels the instance. Once cancelled, a
DownloadRequestcan no longer be resumed or suspended.Note
This method will NOT produce resume data. If you wish to cancel and produce resume data, use
cancel(producingResumeData:)orcancel(byProducingResumeData:).Declaration
Swift
@discardableResult override public func cancel() -> SelfReturn Value
The instance.
-
Cancels the instance, optionally producing resume data. Once cancelled, a
DownloadRequestcan no longer be resumed or suspended.Note
If
producingResumeDataistrue, theresumeDataproperty will be populated with any resume data, if available.Declaration
Swift
@discardableResult public func cancel(producingResumeData shouldProduceResumeData: Bool) -> SelfReturn Value
The instance.
-
Cancels the instance while producing resume data. Once cancelled, a
DownloadRequestcan no longer be resumed or suspended.Note
The resume data passed to the completion handler will also be available on the instance’s
resumeDataproperty.Declaration
Swift
@discardableResult @preconcurrency public func cancel(byProducingResumeData completionHandler: @escaping @Sendable (_ data: Data?) -> Void) -> SelfParameters
completionHandlerThe completion handler that is called when the download has been successfully cancelled. It is not guaranteed to be called on a particular queue, so you may want use an appropriate queue to perform your work.
Return Value
The instance.
-
Validates the request, using the specified closure.
Note
If validation fails, subsequent calls to response handlers will have an associated error.
Declaration
Swift
@discardableResult public func validate(_ validation: @escaping Validation) -> SelfParameters
validationValidationclosure to validate the response.Return Value
The instance.
-
Adds a handler to be called once the request has finished.
Declaration
Swift
@discardableResult @preconcurrency public func response(queue: DispatchQueue = .main, completionHandler: @escaping @Sendable (AFDownloadResponse<URL?>) -> Void) -> SelfParameters
queueThe queue on which the completion handler is dispatched.
.mainby default.completionHandlerThe code to be executed once the request has finished.
Return Value
The request.
-
Adds a handler to be called once the request has finished.
Note
This handler will read the entire downloaded file into memory, use with caution.
Declaration
Swift
@discardableResult public func response<Serializer: DownloadResponseSerializerProtocol>(queue: DispatchQueue = .main, responseSerializer: Serializer, completionHandler: @escaping @Sendable (AFDownloadResponse<Serializer.SerializedObject>) -> Void) -> SelfParameters
queueThe queue on which the completion handler is dispatched.
.mainby default.responseSerializerThe response serializer responsible for serializing the request, response, and data contained in the destination
URL.completionHandlerThe code to be executed once the request has finished.
Return Value
The request.
-
Adds a handler to be called once the request has finished.
Note
This handler will read the entire downloaded file into memory, use with caution.
Declaration
Swift
@discardableResult public func response<Serializer: ResponseSerializer>(queue: DispatchQueue = .main, responseSerializer: Serializer, completionHandler: @escaping @Sendable (AFDownloadResponse<Serializer.SerializedObject>) -> Void) -> SelfParameters
queueThe queue on which the completion handler is dispatched.
.mainby default.responseSerializerThe response serializer responsible for serializing the request, response, and data contained in the destination
URL.completionHandlerThe code to be executed once the request has finished.
Return Value
The request.
-
Adds a handler using a
URLResponseSerializerto be called once the request is finished.Declaration
Swift
@discardableResult @preconcurrency public func responseURL(queue: DispatchQueue = .main, completionHandler: @escaping @Sendable (AFDownloadResponse<URL>) -> Void) -> SelfParameters
queueThe queue on which the completion handler is called.
.mainby default.completionHandlerA closure to be executed once the request has finished.
Return Value
The request.
-
Adds a handler using a
DataResponseSerializerto be called once the request has finished.Note
This handler will read the entire downloaded file into memory, use with caution.
Declaration
Swift
@discardableResult @preconcurrency public func responseData(queue: DispatchQueue = .main, dataPreprocessor: any DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor, emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods, completionHandler: @escaping @Sendable (AFDownloadResponse<Data>) -> Void) -> SelfParameters
queueThe queue on which the completion handler is called.
.mainby default.dataPreprocessorDataPreprocessorwhich processes the receivedDatabefore calling thecompletionHandler.PassthroughPreprocessor()by default.emptyResponseCodesHTTP status codes for which empty responses are always valid.
[204, 205]by default.emptyRequestMethodsHTTPMethods for which empty responses are always valid.[.head]by default.completionHandlerA closure to be executed once the request has finished.
Return Value
The request.
-
responseString(queue:dataPreprocessor: encoding: emptyResponseCodes: emptyRequestMethods: completionHandler: ) Adds a handler using a
StringResponseSerializerto be called once the request has finished.Note
This handler will read the entire downloaded file into memory, use with caution.
Declaration
Swift
@discardableResult @preconcurrency public func responseString(queue: DispatchQueue = .main, dataPreprocessor: any DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor, encoding: String.Encoding? = nil, emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods, completionHandler: @escaping @Sendable (AFDownloadResponse<String>) -> Void) -> SelfParameters
queueThe queue on which the completion handler is dispatched.
.mainby default.dataPreprocessorDataPreprocessorwhich processes the receivedDatabefore calling thecompletionHandler.PassthroughPreprocessor()by default.encodingThe string encoding. Defaults to
nil, in which case the encoding will be determined from the server response, falling back to the default HTTP character set,ISO-8859-1.emptyResponseCodesHTTP status codes for which empty responses are always valid.
[204, 205]by default.emptyRequestMethodsHTTPMethods for which empty responses are always valid.[.head]by default.completionHandlerA closure to be executed once the request has finished.
Return Value
The request.
-
responseJSON(queue:dataPreprocessor: emptyResponseCodes: emptyRequestMethods: options: completionHandler: ) Adds a handler using a
JSONResponseSerializerto be called once the request has finished.Note
This handler will read the entire downloaded file into memory, use with caution.
Declaration
Swift
@available(*, deprecated, message: "responseJSON deprecated and will be removed in Alamofire 6. Use responseDecodable instead.") @discardableResult @preconcurrency public func responseJSON(queue: DispatchQueue = .main, dataPreprocessor: any DataPreprocessor = JSONResponseSerializer.defaultDataPreprocessor, emptyResponseCodes: Set<Int> = JSONResponseSerializer.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = JSONResponseSerializer.defaultEmptyRequestMethods, options: JSONSerialization.ReadingOptions = .allowFragments, completionHandler: @escaping @Sendable (AFDownloadResponse<Any>) -> Void) -> SelfParameters
queueThe queue on which the completion handler is dispatched.
.mainby default.dataPreprocessorDataPreprocessorwhich processes the receivedDatabefore calling thecompletionHandler.PassthroughPreprocessor()by default.emptyResponseCodesHTTP status codes for which empty responses are always valid.
[204, 205]by default.emptyRequestMethodsHTTPMethods for which empty responses are always valid.[.head]by default.optionsJSONSerialization.ReadingOptionsused when parsing the response..allowFragmentsby default.completionHandlerA closure to be executed once the request has finished.
Return Value
The request.
-
responseDecodable(of:queue: dataPreprocessor: decoder: emptyResponseCodes: emptyRequestMethods: completionHandler: ) Adds a handler using a
DecodableResponseSerializerto be called once the request has finished.Note
This handler will read the entire downloaded file into memory, use with caution.
Declaration
Swift
@discardableResult @preconcurrency public func responseDecodable<T: Decodable>(of type: T.Type = T.self, queue: DispatchQueue = .main, dataPreprocessor: any DataPreprocessor = DecodableResponseSerializer<T>.defaultDataPreprocessor, decoder: any DataDecoder = JSONDecoder(), emptyResponseCodes: Set<Int> = DecodableResponseSerializer<T>.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<T>.defaultEmptyRequestMethods, completionHandler: @escaping @Sendable (AFDownloadResponse<T>) -> Void) -> Self where T: SendableParameters
typeDecodabletype to decode from response data.queueThe queue on which the completion handler is dispatched.
.mainby default.dataPreprocessorDataPreprocessorwhich processes the receivedDatabefore calling thecompletionHandler.PassthroughPreprocessor()by default.decoderDataDecoderto use to decode the response.JSONDecoder()by default.emptyResponseCodesHTTP status codes for which empty responses are always valid.
[204, 205]by default.emptyRequestMethodsHTTPMethods for which empty responses are always valid.[.head]by default.completionHandlerA closure to be executed once the request has finished.
Return Value
The request.
-
Creates a
DownloadResponsePublisherfor this instance using the givenResponseSerializerandDispatchQueue.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishResponse<Serializer: ResponseSerializer, T>(using serializer: Serializer, on queue: DispatchQueue = .main) -> DownloadResponsePublisher<T> where Serializer.SerializedObject == TParameters
serializerResponseSerializerused to serialize the responseDatafrom disk.queueDispatchQueueon which theDownloadResponsewill be published..mainby default.Return Value
-
Creates a
DownloadResponsePublisherfor this instance using the givenDownloadResponseSerializerProtocolandDispatchQueue.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishResponse<Serializer: DownloadResponseSerializerProtocol, T>(using serializer: Serializer, on queue: DispatchQueue = .main) -> DownloadResponsePublisher<T> where Serializer.SerializedObject == TParameters
serializerDownloadResponseSerializerused to serialize the responseDatafrom disk.queueDispatchQueueon which theDownloadResponsewill be published..mainby default.Return Value
-
Creates a
DownloadResponsePublisherfor this instance and uses aURLResponseSerializerto serialize the response.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishURL(queue: DispatchQueue = .main) -> DownloadResponsePublisher<URL>Parameters
queueDispatchQueueon which theDownloadResponsewill be published..mainby default.Return Value
-
Creates a
DownloadResponsePublisherfor this instance and uses aDataResponseSerializerto serialize the response.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishData(queue: DispatchQueue = .main, preprocessor: any DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor, emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods) -> DownloadResponsePublisher<Data>Parameters
queueDispatchQueueon which theDownloadResponsewill be published..mainby default.preprocessorDataPreprocessorwhich filters theDatabefore serialization.PassthroughPreprocessor()by default.emptyResponseCodesSet<Int>of HTTP status codes for which empty responses are allowed.[204, 205]by default.emptyRequestMethodsSet<HTTPMethod>ofHTTPMethods for which empty responses are allowed, regardless of status code.[.head]by default.Return Value
-
Creates a
DownloadResponsePublisherfor this instance and uses aStringResponseSerializerto serialize the response.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishString(queue: DispatchQueue = .main, preprocessor: any DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor, encoding: String.Encoding? = nil, emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods) -> DownloadResponsePublisher<String>Parameters
queueDispatchQueueon which theDataResponsewill be published..mainby default.preprocessorDataPreprocessorwhich filters theDatabefore serialization.PassthroughPreprocessor()by default.encodingString.Encodingto parse the response.nilby default, in which case the encoding will be determined by the server response, falling back to the default HTTP character set,ISO-8859-1.emptyResponseCodesSet<Int>of HTTP status codes for which empty responses are allowed.[204, 205]by default.emptyRequestMethodsSet<HTTPMethod>ofHTTPMethods for which empty responses are allowed, regardless of status code.[.head]by default.Return Value
-
Undocumented
Declaration
Swift
@_disfavoredOverload public func publishDecodable<T: Decodable>(type: T.Type = T.self, queue: DispatchQueue = .main, preprocessor: any DataPreprocessor = DecodableResponseSerializer<T>.defaultDataPreprocessor, decoder: any DataDecoder = JSONDecoder(), emptyResponseCodes: Set<Int> = DecodableResponseSerializer<T>.defaultEmptyResponseCodes, emptyResponseMethods: Set<HTTPMethod> = DecodableResponseSerializer<T>.defaultEmptyRequestMethods) -> DownloadResponsePublisher<T> -
Creates a
DownloadResponsePublisherfor this instance and uses aDecodableResponseSerializerto serialize the response.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishDecodable<T: Decodable>(type: T.Type = T.self, queue: DispatchQueue = .main, preprocessor: any DataPreprocessor = DecodableResponseSerializer<T>.defaultDataPreprocessor, decoder: any DataDecoder = JSONDecoder(), emptyResponseCodes: Set<Int> = DecodableResponseSerializer<T>.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<T>.defaultEmptyRequestMethods) -> DownloadResponsePublisher<T>Parameters
typeDecodabletype to which to decode responseData. Inferred from the context by default.queueDispatchQueueon which theDataResponsewill be published..mainby default.preprocessorDataPreprocessorwhich filters theDatabefore serialization.PassthroughPreprocessor()by default.decoderDataDecoderinstance used to decode responseData.JSONDecoder()by default.emptyResponseCodesSet<Int>of HTTP status codes for which empty responses are allowed.[204, 205]by default.emptyRequestMethodsSet<HTTPMethod>ofHTTPMethods for which empty responses are allowed, regardless of status code.[.head]by default.Return Value
-
Creates a
DownloadResponsePublisherfor this instance which does not serialize the response before publishing.Declaration
Swift
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) public func publishUnserialized(on queue: DispatchQueue = .main) -> DownloadResponsePublisher<URL?>Parameters
queueDispatchQueueon which theDownloadResponsewill be published..mainby default.Return Value
-
Creates a
DownloadTasktoawaitaDatavalue.Declaration
Swift
public func serializingData(automaticallyCancelling shouldAutomaticallyCancel: Bool = true, dataPreprocessor: any DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor, emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods) -> DownloadTask<Data>Parameters
shouldAutomaticallyCancelBooldetermining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies toDownloadTask‘s async properties.trueby default.dataPreprocessorDataPreprocessorwhich processes the receivedDatabefore completion.emptyResponseCodesHTTP response codes for which empty responses are allowed.
[204, 205]by default.emptyRequestMethodsHTTPMethods for which empty responses are always valid.[.head]by default.Return Value
The
DownloadTask. -
serializingDecodable(_:automaticallyCancelling: dataPreprocessor: decoder: emptyResponseCodes: emptyRequestMethods: ) Creates a
DownloadTasktoawaitserialization of aDecodablevalue.Note
This serializer reads the entire response into memory before parsing.
Declaration
Swift
public func serializingDecodable<Value: Decodable>(_ type: Value.Type = Value.self, automaticallyCancelling shouldAutomaticallyCancel: Bool = true, dataPreprocessor: any DataPreprocessor = DecodableResponseSerializer<Value>.defaultDataPreprocessor, decoder: any DataDecoder = JSONDecoder(), emptyResponseCodes: Set<Int> = DecodableResponseSerializer<Value>.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<Value>.defaultEmptyRequestMethods) -> DownloadTask<Value>Parameters
typeDecodabletype to decode from response data.shouldAutomaticallyCancelBooldetermining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies toDownloadTask‘s async properties.trueby default.dataPreprocessorDataPreprocessorwhich processes the receivedDatabefore calling the serializer.PassthroughPreprocessor()by default.decoderDataDecoderto use to decode the response.JSONDecoder()by default.emptyResponseCodesHTTP status codes for which empty responses are always valid.
[204, 205]by default.emptyRequestMethodsHTTPMethods for which empty responses are always valid.[.head]by default.Return Value
The
DownloadTask. -
Creates a
DownloadTasktoawaitserialization of the downloaded file’sURLon disk.Declaration
Swift
public func serializingDownloadedFileURL(automaticallyCancelling shouldAutomaticallyCancel: Bool = true) -> DownloadTask<URL>Parameters
shouldAutomaticallyCancelBooldetermining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies toDownloadTask‘s async properties.trueby default.Return Value
The
DownloadTask. -
serializingString(automaticallyCancelling:dataPreprocessor: encoding: emptyResponseCodes: emptyRequestMethods: ) Creates a
DownloadTasktoawaitserialization of aStringvalue.Declaration
Swift
public func serializingString(automaticallyCancelling shouldAutomaticallyCancel: Bool = true, dataPreprocessor: any DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor, encoding: String.Encoding? = nil, emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes, emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods) -> DownloadTask<String>Parameters
shouldAutomaticallyCancelBooldetermining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies toDownloadTask‘s async properties.trueby default.dataPreprocessorDataPreprocessorwhich processes the receivedDatabefore calling the serializer.PassthroughPreprocessor()by default.encodingString.Encodingto use during serialization. Defaults tonil, in which case the encoding will be determined from the server response, falling back to the default HTTP character set,ISO-8859-1.emptyResponseCodesHTTP status codes for which empty responses are always valid.
[204, 205]by default.emptyRequestMethodsHTTPMethods for which empty responses are always valid.[.head]by default.Return Value
The
DownloadTask. -
Creates a
DownloadTasktoawaitserialization using the providedResponseSerializerinstance.Declaration
Swift
public func serializingDownload<Serializer: ResponseSerializer>(using serializer: Serializer, automaticallyCancelling shouldAutomaticallyCancel: Bool = true) -> DownloadTask<Serializer.SerializedObject>Parameters
serializerResponseSerializerresponsible for serializing the request, response, and data.shouldAutomaticallyCancelBooldetermining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies toDownloadTask‘s async properties.trueby default.Return Value
The
DownloadTask. -
Creates a
DownloadTasktoawaitserialization using the providedDownloadResponseSerializerProtocolinstance.Declaration
Swift
public func serializingDownload<Serializer: DownloadResponseSerializerProtocol>(using serializer: Serializer, automaticallyCancelling shouldAutomaticallyCancel: Bool = true) -> DownloadTask<Serializer.SerializedObject>Parameters
serializerDownloadResponseSerializerProtocolresponsible for serializing the request, response, and data.shouldAutomaticallyCancelBooldetermining whether or not the request should be cancelled when the enclosing async context is cancelled. Only applies toDownloadTask‘s async properties.trueby default.Return Value
The
DownloadTask.
-
A closure used to validate a request that takes a URL request, a URL response, a temporary URL and a destination URL, and returns whether the request was valid.
Declaration
Swift
public typealias Validation = @Sendable (_ request: URLRequest?, _ response: HTTPURLResponse, _ fileURL: URL?) -> ValidationResult -
Validates that the response has a status code in the specified sequence.
If validation fails, subsequent calls to response handlers will have an associated error.
Declaration
Swift
@discardableResult @preconcurrency public func validate<S>(statusCode acceptableStatusCodes: S) -> Self where S : Sendable, S : Sequence, S.Element == IntParameters
acceptableStatusCodesSequenceof acceptable response status codes.Return Value
The instance.
-
Validates that the response has a
Content-Typein the specified sequence.If validation fails, subsequent calls to response handlers will have an associated error.
Declaration
Swift
@discardableResult @preconcurrency public func validate<S>(contentType acceptableContentTypes: @escaping @Sendable @autoclosure () -> S) -> Self where S : Sendable, S : Sequence, S.Element == StringParameters
contentTypeThe acceptable content types, which may specify wildcard types and/or subtypes.
Return Value
The request.
-
Validates that the response has a status code in the default acceptable range of 200…299, and that the content type matches any specified in the Accept HTTP header field.
If validation fails, subsequent calls to response handlers will have an associated error.
Declaration
Swift
@discardableResult public func validate() -> SelfReturn Value
The request.
View on GitHub
Install in Dash