BMSURLSession

public struct BMSURLSession: NetworkSession

Sends HTTP network requests.

BMSURLSession is an alternative to BaseRequest that provides more flexibility and control over requests and their responses.

It is built as a wrapper around Swift’s URLSession API that incorporates Bluemix Mobile Services. It automatically gathers Mobile Analytics data on each network request, and can be used to access backends that are protected by Mobile Client Access.

Currently, BMSURLSession only supports URLSessionDataTask and URLSessionUploadTask.

  • Creates a network session similar to URLSession.

    Declaration

    Swift

    public init(configuration: URLSessionConfiguration = .default,
                delegate: URLSessionDelegate? = nil,
                delegateQueue: OperationQueue? = nil,
                autoRetries: Int = 0)

    Parameters

    configuration

    Defines the behavior of the session.

    delegate

    Handles session-related events. If nil, use task methods that take completion handlers.

    delegateQueue

    Queue for scheduling the delegate calls and completion handlers.

    autoRetries

    The number of times to retry each request if it fails to send. The conditions for retries are: timeout, loss of network connectivity, failure to connect to the host, and 504 responses.

  • Creates a task that retrieves the contents of the specified URL.

    To start the task, you must call its resume() method.

    Declaration

    Swift

    public func dataTask(with url: URL) -> URLSessionDataTask

    Parameters

    url

    The URL to retrieve data from.

    Return Value

    A data task.

  • Creates a task that retrieves the contents of the specified URL, and passes the response to the completion handler.

    To start the task, you must call its resume() method.

    Note

    It is not recommended to use this method if the session was created with a delegate. The completion handler will override all delegate methods except those for handling authentication challenges.

    Declaration

    Swift

    public func dataTask(with url: URL, completionHandler: @escaping BMSDataTaskCompletionHandler) -> URLSessionDataTask

    Parameters

    url

    The URL to retrieve data from.

    completionHandler

    The completion handler to call when the request is complete.

    Return Value

    A data task.

  • Creates a task that uploads data to the URL specified in the request object.

    To start the task, you must call its resume() method.

    Declaration

    Swift

    public func uploadTask(with request: URLRequest, from bodyData: Data) -> URLSessionUploadTask

    Parameters

    request

    An object that provides request-specific information such as the URL and cache policy. The request body is ignored.

    bodyData

    The body data for the request.

    Return Value

    An upload task.

  • Creates a task that uploads data to the URL specified in the request object.

    To start the task, you must call its resume() method.

    Note

    It is not recommended to use this method if the session was created with a delegate. The completion handler will override all delegate methods except those for handling authentication challenges.

    Declaration

    Swift

    public func uploadTask(with request: URLRequest, from bodyData: Data?, completionHandler: @escaping BMSDataTaskCompletionHandler) -> URLSessionUploadTask

    Parameters

    request

    An object that provides request-specific information such as the URL and cache policy. The request body is ignored.

    bodyData

    The body data for the request.

    completionHandler

    The completion handler to call when the request is complete.

    Return Value

    An upload task.

  • Creates a task that uploads a file to the URL specified in the request object.

    To start the task, you must call its resume() method.

    Declaration

    Swift

    public func uploadTask(with request: URLRequest, fromFile fileURL: URL) -> URLSessionUploadTask

    Parameters

    request

    An object that provides request-specific information such as the URL and cache policy. The request body is ignored.

    fileURL

    The location of the file to upload.

    Return Value

    An upload task.

  • Creates a task that uploads a file to the URL specified in the request object.

    To start the task, you must call its resume() method.

    Note

    It is not recommended to use this method if the session was created with a delegate. The completion handler will override all delegate methods except those for handling authentication challenges.

    Declaration

    Swift

    public func uploadTask(with request: URLRequest, fromFile fileURL: URL, completionHandler: @escaping BMSDataTaskCompletionHandler) -> URLSessionUploadTask

    Parameters

    request

    An object that provides request-specific information such as the URL and cache policy. The request body is ignored.

    fileURL

    The location of the file to upload.

    completionHandler

    The completion handler to call when the request is complete.

    Return Value

    An upload task.