Manager

public class Manager

Responsible for creating and managing Request objects, as well as their underlying NSURLSession.

  • A shared instance of Manager, used by top-level Alamofire request methods, and suitable for use directly for any ad hoc requests.

    Declaration

    Swift

    public static let sharedInstance: Manager =
  • Creates default values for the Accept-Encoding, Accept-Language and User-Agent headers.

    Declaration

    Swift

    public static let defaultHTTPHeaders: [String: String] =
  • The underlying session.

    Declaration

    Swift

    public let session: NSURLSession
  • 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

    public var startRequestsImmediately: Bool = true
  • The background completion handler closure provided by the UIApplicationDelegate application:handleEventsForBackgroundURLSession:completionHandler: method. By setting the background completion handler, the SessionDelegate sessionDidFinishEventsForBackgroundURLSession 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

    public var backgroundCompletionHandler: (() -> Void)?
  • Initializes the Manager instance with the specified configuration, delegate and server trust policy.

    Declaration

    Swift

    public init(
            configuration: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration(),
            delegate: SessionDelegate = SessionDelegate(),
            serverTrustPolicyManager: ServerTrustPolicyManager? = nil)

    Parameters

    configuration

    The configuration used to construct the managed session. NSURLSessionConfiguration.defaultSessionConfiguration() 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 Manager instance.

  • Initializes the Manager instance with the specified session, delegate and server trust policy.

    Declaration

    Swift

    public init?(
            session: NSURLSession,
            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 Manager instance if the URL session’s delegate matches the delegate parameter.

  • Creates a request for the specified method, URL string, parameters, parameter encoding and headers.

    Declaration

    Swift

    public func request(
            method: Method,
            _ URLString: URLStringConvertible,
            parameters: [String: AnyObject]? = nil,
            encoding: ParameterEncoding = .URL,
            headers: [String: String]? = nil)
            -> Request

    Parameters

    method

    The HTTP method.

    URLString

    The URL string.

    parameters

    The parameters. nil by default.

    encoding

    The parameter encoding. .URL by default.

    headers

    The HTTP headers. nil by default.

    Return Value

    The created request.

  • Creates a request for the specified URL request.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func request(URLRequest: URLRequestConvertible) -> Request

    Parameters

    URLRequest

    The URL request

    Return Value

    The created request.

  • Responsible for handling all delegate callbacks for the underlying session.

    See more

    Declaration

    Swift

    public class SessionDelegate: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate
  • Creates a download request for the specified method, URL string, parameters, parameter encoding, headers and destination.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func download(
            method: Method,
            _ URLString: URLStringConvertible,
            parameters: [String: AnyObject]? = nil,
            encoding: ParameterEncoding = .URL,
            headers: [String: String]? = nil,
            destination: Request.DownloadFileDestination)
            -> Request

    Parameters

    method

    The HTTP method.

    URLString

    The URL string.

    parameters

    The parameters. nil by default.

    encoding

    The parameter encoding. .URL by default.

    headers

    The HTTP headers. nil by default.

    destination

    The closure used to determine the destination of the downloaded file.

    Return Value

    The created download request.

  • Creates a request for downloading from the specified URL request.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func download(URLRequest: URLRequestConvertible, destination: Request.DownloadFileDestination) -> Request

    Parameters

    URLRequest

    The URL request

    destination

    The closure used to determine the destination of the downloaded file.

    Return Value

    The created download request.

  • Creates a request for downloading from the resume data produced from a previous request cancellation.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func download(resumeData: NSData, destination: Request.DownloadFileDestination) -> Request

    Parameters

    resumeData

    The resume data. This is an opaque data blob produced by NSURLSessionDownloadTask when a task is cancelled. See NSURLSession -downloadTaskWithResumeData: for additional information.

    destination

    The closure used to determine the destination of the downloaded file.

    Return Value

    The created download request.

  • Creates a request for bidirectional streaming with the given hostname and port.

    :returns: The created stream request.

    Declaration

    Swift

    public func stream(hostName hostName: String, port: Int) -> Request

    Parameters

    hostName

    The hostname of the server to connect to.

    port

    The port of the server to connect to.

  • Creates a request for bidirectional streaming with the given NSNetService.

    Declaration

    Swift

    public func stream(netService netService: NSNetService) -> Request

    Parameters

    netService

    The net service used to identify the endpoint.

    Return Value

    The created stream request.

  • Overrides default behavior for NSURLSessionStreamDelegate method URLSession:readClosedForStreamTask:.

    Declaration

    Swift

    public var streamTaskReadClosed: ((NSURLSession, NSURLSessionStreamTask) -> Void)?
  • Overrides default behavior for NSURLSessionStreamDelegate method URLSession:writeClosedForStreamTask:.

    Declaration

    Swift

    public var streamTaskWriteClosed: ((NSURLSession, NSURLSessionStreamTask) -> Void)?
  • Overrides default behavior for NSURLSessionStreamDelegate method URLSession:betterRouteDiscoveredForStreamTask:.

    Declaration

    Swift

    public var streamTaskBetterRouteDiscovered: ((NSURLSession, NSURLSessionStreamTask) -> Void)?
  • Overrides default behavior for NSURLSessionStreamDelegate method URLSession:streamTask:didBecomeInputStream:outputStream:.

    Declaration

    Swift

    public var streamTaskDidBecomeInputStream: ((NSURLSession, NSURLSessionStreamTask, NSInputStream, NSOutputStream) -> Void)?
  • Tells the delegate that the read side of the connection has been closed.

    Declaration

    Swift

    public func URLSession(session: NSURLSession, readClosedForStreamTask streamTask: NSURLSessionStreamTask)

    Parameters

    session

    The session.

    streamTask

    The stream task.

  • Tells the delegate that the write side of the connection has been closed.

    Declaration

    Swift

    public func URLSession(session: NSURLSession, writeClosedForStreamTask streamTask: NSURLSessionStreamTask)

    Parameters

    session

    The session.

    streamTask

    The stream task.

  • Tells the delegate that the system has determined that a better route to the host is available.

    Declaration

    Swift

    public func URLSession(session: NSURLSession, betterRouteDiscoveredForStreamTask streamTask: NSURLSessionStreamTask)

    Parameters

    session

    The session.

    streamTask

    The stream task.

  • Tells the delegate that the stream task has been completed and provides the unopened stream objects.

    Declaration

    Swift

    public func URLSession(
            session: NSURLSession,
            streamTask: NSURLSessionStreamTask,
            didBecomeInputStream inputStream: NSInputStream,
            outputStream: NSOutputStream)

    Parameters

    session

    The session.

    streamTask

    The stream task.

    inputStream

    The new input stream.

    outputStream

    The new output stream.

  • Creates a request for uploading a file to the specified URL request.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request

    Parameters

    URLRequest

    The URL request

    file

    The file to upload

    Return Value

    The created upload request.

  • Creates a request for uploading a file to the specified URL request.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func upload(
            method: Method,
            _ URLString: URLStringConvertible,
            headers: [String: String]? = nil,
            file: NSURL)
            -> Request

    Parameters

    method

    The HTTP method.

    URLString

    The URL string.

    headers

    The HTTP headers. nil by default.

    file

    The file to upload

    Return Value

    The created upload request.

  • Creates a request for uploading data to the specified URL request.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request

    Parameters

    URLRequest

    The URL request.

    data

    The data to upload.

    Return Value

    The created upload request.

  • Creates a request for uploading data to the specified URL request.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func upload(
            method: Method,
            _ URLString: URLStringConvertible,
            headers: [String: String]? = nil,
            data: NSData)
            -> Request

    Parameters

    method

    The HTTP method.

    URLString

    The URL string.

    headers

    The HTTP headers. nil by default.

    data

    The data to upload

    Return Value

    The created upload request.

  • Creates a request for uploading a stream to the specified URL request.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request

    Parameters

    URLRequest

    The URL request.

    stream

    The stream to upload.

    Return Value

    The created upload request.

  • Creates a request for uploading a stream to the specified URL request.

    If startRequestsImmediately is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func upload(
            method: Method,
            _ URLString: URLStringConvertible,
            headers: [String: String]? = nil,
            stream: NSInputStream)
            -> Request

    Parameters

    method

    The HTTP method.

    URLString

    The URL string.

    headers

    The HTTP headers. nil by default.

    stream

    The stream to upload.

    Return Value

    The created upload request.

  • Default memory threshold used when encoding MultipartFormData.

    Declaration

    Swift

    public static let MultipartFormDataEncodingMemoryThreshold: UInt64 = 10 * 1024 * 1024
  • 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 new Request along with streaming information.
    • Failure: Used to represent a failure in the MultipartFormData encoding and also contains the encoding error.
    See more

    Declaration

    Swift

    public enum MultipartFormDataEncodingResult
  • Encodes the MultipartFormData and creates a request to upload the result to the specified URL request.

    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 the MultipartFormData is below the encodingMemoryThreshold, 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 is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func upload(
            method: Method,
            _ URLString: URLStringConvertible,
            headers: [String: String]? = nil,
            multipartFormData: MultipartFormData -> Void,
            encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
            encodingCompletion: (MultipartFormDataEncodingResult -> Void)?)

    Parameters

    method

    The HTTP method.

    URLString

    The URL string.

    headers

    The HTTP headers. nil by default.

    multipartFormData

    The closure used to append body parts to the MultipartFormData.

    encodingMemoryThreshold

    The encoding memory threshold in bytes. MultipartFormDataEncodingMemoryThreshold by default.

    encodingCompletion

    The closure called when the MultipartFormData encoding is complete.

  • Encodes the MultipartFormData and creates a request to upload the result to the specified URL request.

    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 the MultipartFormData is below the encodingMemoryThreshold, 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 is true, the request will have resume() called before being returned.

    Declaration

    Swift

    public func upload(
            URLRequest: URLRequestConvertible,
            multipartFormData: MultipartFormData -> Void,
            encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
            encodingCompletion: (MultipartFormDataEncodingResult -> Void)?)

    Parameters

    URLRequest

    The URL request.

    multipartFormData

    The closure used to append body parts to the MultipartFormData.

    encodingMemoryThreshold

    The encoding memory threshold in bytes. MultipartFormDataEncodingMemoryThreshold by default.

    encodingCompletion

    The closure called when the MultipartFormData encoding is complete.