You are viewing documentation for a previous version of this software.

Switch to the latest stable version.

URLSessionClient


CLASS

URLSessionClient

Swift
1open class URLSessionClient: NSObject, URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate

A class to handle URL Session calls that will support background execution, but still (mostly) use callbacks for its primary method of communication.

NOTE: Delegate methods implemented here are not documented inline because Apple has their own documentation for them. Please consult Apple's documentation for how the delegate methods work and what needs to be overridden and handled within your app, particularly in regards to what needs to be called when for background sessions.

Properties

session

Swift
1open private(set) var session: URLSession!

The raw URLSession being used for this client

Methods

init(sessionConfiguration:callbackQueue:)

Swift
1public init(sessionConfiguration: URLSessionConfiguration = .default,
2            callbackQueue: OperationQueue? = .main)

Designated initializer.

  • Parameters:

    • sessionConfiguration: The URLSessionConfiguration to use to set up the URL session.

    • callbackQueue: [optional] The OperationQueue to tell the URL session to call back to this class on, which will in turn call back to your class. Defaults to .main.

Parameters

NameDescription
sessionConfigurationThe URLSessionConfiguration to use to set up the URL session.
callbackQueue[optional] The OperationQueue to tell the URL session to call back to this class on, which will in turn call back to your class. Defaults to .main.

invalidate()

Swift
1public func invalidate()

Cleans up and invalidates everything related to this session client.

NOTE: This must be called from the deinit of anything holding onto this client in order to break a retain cycle with the delegate.

clear(task:)

Swift
1open func clear(task identifier: Int)

Clears underlying dictionaries of any data related to a particular task identifier.

  • Parameter identifier: The identifier of the task to clear.

Parameters

NameDescription
identifierThe identifier of the task to clear.

clearAllTasks()

Swift
1open func clearAllTasks()

Clears underlying dictionaries of any data related to all tasks.

Mostly useful for cleanup and/or after invalidation of the URLSession.

sendRequest(_:rawTaskCompletionHandler:completion:)

Swift
1open func sendRequest(_ request: URLRequest,
2                      rawTaskCompletionHandler: RawCompletion? = nil,
3                      completion: @escaping Completion) -> URLSessionTask

The main method to perform a request.

  • Parameters:

    • request: The request to perform.

    • rawTaskCompletionHandler: [optional] A completion handler to call once the raw task is done, so if an Error requires access to the headers, the user can still access these.

    • completion: A completion handler to call when the task has either completed successfully or failed.

  • Returns: The created URLSession task, already resumed, because nobody ever remembers to call resume().

Parameters

NameDescription
requestThe request to perform.
rawTaskCompletionHandler[optional] A completion handler to call once the raw task is done, so if an Error requires access to the headers, the user can still access these.
completionA completion handler to call when the task has either completed successfully or failed.

cancel(task:)

Swift
1open func cancel(task: URLSessionTask)

Cancels a given task and clears out its underlying data.

NOTE: You will not receive any kind of "This was cancelled" error when this is called.

  • Parameter task: The task you wish to cancel.

Parameters

NameDescription
taskThe task you wish to cancel.

urlSession(_:didBecomeInvalidWithError:)

Swift
1open func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?)

urlSession(_:task:didFinishCollecting:)

Swift
1open func urlSession(_ session: URLSession,
2                     task: URLSessionTask,
3                     didFinishCollecting metrics: URLSessionTaskMetrics)

urlSession(_:didReceive:completionHandler:)

Swift
1open func urlSession(_ session: URLSession,
2                     didReceive challenge: URLAuthenticationChallenge,
3                     completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

urlSessionDidFinishEvents(forBackgroundURLSession:)

urlSession(_:task:didReceive:completionHandler:)

Swift
1open func urlSession(_ session: URLSession,
2                     task: URLSessionTask,
3                     didReceive challenge: URLAuthenticationChallenge,
4                     completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)

urlSession(_:taskIsWaitingForConnectivity:)

Swift
1open func urlSession(_ session: URLSession,
2                     taskIsWaitingForConnectivity task: URLSessionTask)

urlSession(_:task:didCompleteWithError:)

Swift
1open func urlSession(_ session: URLSession,
2                     task: URLSessionTask,
3                     didCompleteWithError error: Error?)

urlSession(_:task:needNewBodyStream:)

Swift
1open func urlSession(_ session: URLSession,
2                     task: URLSessionTask,
3                     needNewBodyStream completionHandler: @escaping (InputStream?) -> Void)

urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)

Swift
1open func urlSession(_ session: URLSession,
2                     task: URLSessionTask,
3                     didSendBodyData bytesSent: Int64,
4                     totalBytesSent: Int64,
5                     totalBytesExpectedToSend: Int64)

urlSession(_:task:willBeginDelayedRequest:completionHandler:)

Swift
1open func urlSession(_ session: URLSession,
2                     task: URLSessionTask,
3                     willBeginDelayedRequest request: URLRequest,
4                     completionHandler: @escaping (URLSession.DelayedRequestDisposition, URLRequest?) -> Void)

urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)

Swift
1open func urlSession(_ session: URLSession,
2                     task: URLSessionTask,
3                     willPerformHTTPRedirection response: HTTPURLResponse,
4                     newRequest request: URLRequest,
5                     completionHandler: @escaping (URLRequest?) -> Void)

urlSession(_:dataTask:didReceive:)

Swift
1open func urlSession(_ session: URLSession,
2                     dataTask: URLSessionDataTask,
3                     didReceive data: Data)

urlSession(_:dataTask:didBecome:)

Swift
1open func urlSession(_ session: URLSession,
2                     dataTask: URLSessionDataTask,
3                     didBecome streamTask: URLSessionStreamTask)

urlSession(_:dataTask:didBecome:)

Swift
1open func urlSession(_ session: URLSession,
2                     dataTask: URLSessionDataTask,
3                     didBecome downloadTask: URLSessionDownloadTask)

urlSession(_:dataTask:willCacheResponse:completionHandler:)

Swift
1open func urlSession(_ session: URLSession,
2                     dataTask: URLSessionDataTask,
3                     willCacheResponse proposedResponse: CachedURLResponse,
4                     completionHandler: @escaping (CachedURLResponse?) -> Void)

urlSession(_:dataTask:didReceive:completionHandler:)

Swift
1open func urlSession(_ session: URLSession,
2                     dataTask: URLSessionDataTask,
3                     didReceive response: URLResponse,
4                     completionHandler: @escaping (URLSession.ResponseDisposition) -> Void)
Feedback

Edit on GitHub

Forums