SessionManager
open class SessionManager
Responsible for creating and managing Request
objects, as well as their underlying NSURLSession
.
-
Defines whether the
MultipartFormData
encoding was successful and contains result of the encoding as associated values.- Success: Represents a successful
MultipartFormData
encoding and contains the newUploadRequest
along with streaming information. - Failure: Used to represent a failure in the
MultipartFormData
encoding and also contains the encoding error.
Declaration
Swift
public enum MultipartFormDataEncodingResult
- Success: Represents a successful
-
A default instance of
SessionManager
, used by top-level Alamofire request methods, and suitable for use directly for any ad hoc requests.Declaration
Swift
public static let `default`: SessionManager
-
Creates default values for the “Accept-Encoding”, “Accept-Language” and “User-Agent” headers.
Declaration
Swift
public static let defaultHTTPHeaders: HTTPHeaders
-
Default memory threshold used when encoding
MultipartFormData
in bytes.Declaration
Swift
public static let multipartFormDataEncodingMemoryThreshold: UInt64
-
The underlying session.
Declaration
Swift
public let session: URLSession
-
The session delegate handling all the task and session delegate callbacks.
Declaration
Swift
public let delegate: SessionDelegate
-
Whether to start requests immediately after being constructed.
true
by default.Declaration
Swift
open var startRequestsImmediately: Bool
-
The request adapter called each time a new request is created.
Declaration
Swift
open var adapter: RequestAdapter?
-
The request retrier called each time a request encounters an error to determine whether to retry the request.
Declaration
Swift
open var retrier: RequestRetrier? { get set }
-
The background completion handler closure provided by the UIApplicationDelegate
application:handleEventsForBackgroundURLSession:completionHandler:
method. By setting the background completion handler, the SessionDelegatesessionDidFinishEventsForBackgroundURLSession
closure implementation will automatically call the handler.If you need to handle your own events before the handler is called, then you need to override the SessionDelegate
sessionDidFinishEventsForBackgroundURLSession
and manually call the handler when finished.nil
by default.Declaration
Swift
open var backgroundCompletionHandler: (() -> Void)?
-
Creates an instance with the specified
configuration
,delegate
andserverTrustPolicyManager
.Declaration
Swift
public init( configuration: URLSessionConfiguration = URLSessionConfiguration.default, delegate: SessionDelegate = SessionDelegate(), serverTrustPolicyManager: ServerTrustPolicyManager? = nil)
Parameters
configuration
The configuration used to construct the managed session.
URLSessionConfiguration.default
by default.delegate
The delegate used when initializing the session.
SessionDelegate()
by default.serverTrustPolicyManager
The server trust policy manager to use for evaluating all server trust challenges.
nil
by default.Return Value
The new
SessionManager
instance. -
Declaration
Swift
public init?( session: URLSession, delegate: SessionDelegate, serverTrustPolicyManager: ServerTrustPolicyManager? = nil)
Parameters
session
The URL session.
delegate
The delegate of the URL session. Must equal the URL session’s delegate.
serverTrustPolicyManager
The server trust policy manager to use for evaluating all server trust challenges.
nil
by default.Return Value
The new
SessionManager
instance if the URL session’s delegate matches;nil
otherwise.
-
Creates a
DataRequest
to retrieve the contents of the specifiedurl
,method
,parameters
,encoding
andheaders
.Declaration
Swift
@discardableResult open func request( _ url: URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: HTTPHeaders? = nil) -> DataRequest
Parameters
url
The URL.
method
The HTTP method.
.get
by default.parameters
The parameters.
nil
by default.encoding
The parameter encoding.
URLEncoding.default
by default.headers
The HTTP headers.
nil
by default.Return Value
The created
DataRequest
. -
Creates a
DataRequest
to retrieve the contents of a URL based on the specifiedurlRequest
.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@discardableResult open func request(_ urlRequest: URLRequestConvertible) -> DataRequest
Parameters
urlRequest
The URL request.
Return Value
The created
DataRequest
.
-
Creates a
DownloadRequest
to retrieve the contents the specifiedurl
,method
,parameters
,encoding
,headers
and save them to thedestination
.If
destination
is not specified, the contents will remain in the temporary location determined by the underlying URL session.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@discardableResult open func download( _ url: URLConvertible, method: HTTPMethod = .get, parameters: Parameters? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: HTTPHeaders? = nil, to destination: DownloadRequest.DownloadFileDestination? = nil) -> DownloadRequest
Parameters
url
The URL.
method
The HTTP method.
.get
by default.parameters
The parameters.
nil
by default.encoding
The parameter encoding.
URLEncoding.default
by default.headers
The HTTP headers.
nil
by default.destination
The closure used to determine the destination of the downloaded file.
nil
by default.Return Value
The created
DownloadRequest
. -
Creates a
DownloadRequest
to retrieve the contents of a URL based on the specifiedurlRequest
and save them to thedestination
.If
destination
is not specified, the contents will remain in the temporary location determined by the underlying URL session.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@discardableResult open func download( _ urlRequest: URLRequestConvertible, to destination: DownloadRequest.DownloadFileDestination? = nil) -> DownloadRequest
Parameters
urlRequest
The URL request
destination
The closure used to determine the destination of the downloaded file.
nil
by default.Return Value
The created
DownloadRequest
.
-
Creates a
DownloadRequest
from theresumeData
produced from a previous request cancellation to retrieve the contents of the original request and save them to thedestination
.If
destination
is not specified, the contents will remain in the temporary location determined by the underlying URL session.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.On the latest release of all the Apple platforms (iOS 10, macOS 10.12, tvOS 10, watchOS 3),
resumeData
is broken on background URL session configurations. There’s an underlying bug in theresumeData
generation logic where the data is written incorrectly and will always fail to resume the download. For more information about the bug and possible workarounds, please refer to the following Stack Overflow post:Declaration
Swift
@discardableResult open func download( resumingWith resumeData: Data, to destination: DownloadRequest.DownloadFileDestination? = nil) -> DownloadRequest
Parameters
resumeData
The resume data. This is an opaque data blob produced by
URLSessionDownloadTask
when a task is cancelled. SeeURLSession -downloadTask(withResumeData:)
for additional information.destination
The closure used to determine the destination of the downloaded file.
nil
by default.Return Value
The created
DownloadRequest
.
-
Creates an
UploadRequest
from the specifiedurl
,method
andheaders
for uploading thefile
.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@discardableResult open func upload( _ fileURL: URL, to url: URLConvertible, method: HTTPMethod = .post, headers: HTTPHeaders? = nil) -> UploadRequest
Parameters
file
The file to upload.
url
The URL.
method
The HTTP method.
.post
by default.headers
The HTTP headers.
nil
by default.Return Value
The created
UploadRequest
. -
Creates a
UploadRequest
from the specifiedurlRequest
for uploading thefile
.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@discardableResult open func upload(_ fileURL: URL, with urlRequest: URLRequestConvertible) -> UploadRequest
Parameters
file
The file to upload.
urlRequest
The URL request.
Return Value
The created
UploadRequest
.
-
Creates an
UploadRequest
from the specifiedurl
,method
andheaders
for uploading thedata
.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@discardableResult open func upload( _ data: Data, to url: URLConvertible, method: HTTPMethod = .post, headers: HTTPHeaders? = nil) -> UploadRequest
Parameters
data
The data to upload.
url
The URL.
method
The HTTP method.
.post
by default.headers
The HTTP headers.
nil
by default.Return Value
The created
UploadRequest
. -
Creates an
UploadRequest
from the specifiedurlRequest
for uploading thedata
.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@discardableResult open func upload(_ data: Data, with urlRequest: URLRequestConvertible) -> UploadRequest
Parameters
data
The data to upload.
urlRequest
The URL request.
Return Value
The created
UploadRequest
.
-
Creates an
UploadRequest
from the specifiedurl
,method
andheaders
for uploading thestream
.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@discardableResult open func upload( _ stream: InputStream, to url: URLConvertible, method: HTTPMethod = .post, headers: HTTPHeaders? = nil) -> UploadRequest
Parameters
stream
The stream to upload.
url
The URL.
method
The HTTP method.
.post
by default.headers
The HTTP headers.
nil
by default.Return Value
The created
UploadRequest
. -
Creates an
UploadRequest
from the specifiedurlRequest
for uploading thestream
.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@discardableResult open func upload(_ stream: InputStream, with urlRequest: URLRequestConvertible) -> UploadRequest
Parameters
stream
The stream to upload.
urlRequest
The URL request.
Return Value
The created
UploadRequest
.
-
Encodes
multipartFormData
usingencodingMemoryThreshold
and callsencodingCompletion
with newUploadRequest
using theurl
,method
andheaders
.It is important to understand the memory implications of uploading
MultipartFormData
. If the cummulative payload is small, encoding the data in-memory and directly uploading to a server is the by far the most efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be used for larger payloads such as video content.The
encodingMemoryThreshold
parameter allows Alamofire to automatically determine whether to encode in-memory or stream from disk. If the content length of theMultipartFormData
is below theencodingMemoryThreshold
, encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding technique was used.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
open func upload( multipartFormData: @escaping (MultipartFormData) -> Void, usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold, to url: URLConvertible, method: HTTPMethod = .post, headers: HTTPHeaders? = nil, queue: DispatchQueue? = nil, encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)
Parameters
multipartFormData
The closure used to append body parts to the
MultipartFormData
.encodingMemoryThreshold
The encoding memory threshold in bytes.
multipartFormDataEncodingMemoryThreshold
by default.url
The URL.
method
The HTTP method.
.post
by default.headers
The HTTP headers.
nil
by default.encodingCompletion
The closure called when the
MultipartFormData
encoding is complete. -
Encodes
multipartFormData
usingencodingMemoryThreshold
and callsencodingCompletion
with newUploadRequest
using theurlRequest
.It is important to understand the memory implications of uploading
MultipartFormData
. If the cummulative payload is small, encoding the data in-memory and directly uploading to a server is the by far the most efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be used for larger payloads such as video content.The
encodingMemoryThreshold
parameter allows Alamofire to automatically determine whether to encode in-memory or stream from disk. If the content length of theMultipartFormData
is below theencodingMemoryThreshold
, encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding technique was used.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
open func upload( multipartFormData: @escaping (MultipartFormData) -> Void, usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold, with urlRequest: URLRequestConvertible, queue: DispatchQueue? = nil, encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)
Parameters
multipartFormData
The closure used to append body parts to the
MultipartFormData
.encodingMemoryThreshold
The encoding memory threshold in bytes.
multipartFormDataEncodingMemoryThreshold
by default.urlRequest
The URL request.
encodingCompletion
The closure called when the
MultipartFormData
encoding is complete.
-
Creates a
StreamRequest
for bidirectional streaming using thehostname
andport
.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@available(iOS 9.0, macOS 10.11, tvOS 9.0, *) @discardableResult open func stream(withHostName hostName: String, port: Int) -> StreamRequest
Parameters
hostName
The hostname of the server to connect to.
port
The port of the server to connect to.
Return Value
The created
StreamRequest
.
-
Creates a
StreamRequest
for bidirectional streaming using thenetService
.If
startRequestsImmediately
istrue
, the request will haveresume()
called before being returned.Declaration
Swift
@available(iOS 9.0, macOS 10.11, tvOS 9.0, *) @discardableResult open func stream(with netService: NetService) -> StreamRequest
Parameters
netService
The net service used to identify the endpoint.
Return Value
The created
StreamRequest
. -
A default with cache policy instance of
SessionManager
, used by top-level Alamofire request methods, and suitable for use directly for any ad hoc requests.Declaration
Swift
public static let defaultWithCachePolicy: SessionManager