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
configurationDefines the behavior of the session.
delegateHandles session-related events. If nil, use task methods that take completion handlers.
delegateQueueQueue for scheduling the delegate calls and completion handlers.
autoRetriesThe 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) -> URLSessionDataTaskParameters
urlThe 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) -> URLSessionDataTaskParameters
urlThe URL to retrieve data from.
completionHandlerThe 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) -> URLSessionUploadTaskParameters
requestAn object that provides request-specific information such as the URL and cache policy. The request body is ignored.
bodyDataThe 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) -> URLSessionUploadTaskParameters
requestAn object that provides request-specific information such as the URL and cache policy. The request body is ignored.
bodyDataThe body data for the request.
completionHandlerThe 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) -> URLSessionUploadTaskParameters
requestAn object that provides request-specific information such as the URL and cache policy. The request body is ignored.
fileURLThe 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) -> URLSessionUploadTaskParameters
requestAn object that provides request-specific information such as the URL and cache policy. The request body is ignored.
fileURLThe location of the file to upload.
completionHandlerThe completion handler to call when the request is complete.
Return Value
An upload task.
View on GitHub
BMSURLSession Structure Reference