SAPURLSessionDelegate
public protocol SAPURLSessionDelegate : AnyObject
The SAPURLSessionDelegate protocol describes the methods that SAPURLSession objects call on their delegates to handle session- and task-level events.
-
sapURLSession(_:didBecomeInvalidWithError:)Default implementationTells the URL session that the session has been invalidated. If you invalidate a session by calling its finishTasksAndInvalidate() method, the session waits until after the final task in the session finishes or fails before calling this delegate method. If you call the invalidateAndCancel() method, the session calls this delegate method immediately.
Default Implementation
Tells the URL session that the session has been invalidated.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, didBecomeInvalidWithError error: Error?)Parameters
sessionThe session object that was invalidated.
errorThe error that caused invalidation, or nil if the invalidation was explicit.
-
sapURLSessionDidFinishEvents(forBackgroundURLSession:)Default implementationTells the delegate that all messages enqueued for a session have been delivered.
Default Implementation
Tells the delegate that all messages enqueued for a session have been delivered.
Declaration
Swift
func sapURLSessionDidFinishEvents(forBackgroundURLSession session: SAPURLSession)Parameters
sessionThe session that no longer has any outstanding requests
-
sapURLSession(_:task:willBeginDelayedRequest:completionHandler:)Default implementationTells the delegate that a delayed URL session task will now begin loading. This method is called when a background session task with a delayed start time (as set with the earliestBeginDate property) is ready to start. This delegate method should only be implemented if the request might become stale while waiting for the network load and needs to be replaced by a new request. For loading to continue, the delegate must call the completion handler, passing in a disposition that indicates how the task should proceed. Passing the cancel disposition is equivalent to calling cancel() on the task directly.
Default Implementation
Tells the delegate that a delayed URL session task will now begin loading.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, willBeginDelayedRequest request: URLRequest, completionHandler: @escaping (SAPURLSession.DelayedRequestDisposition) -> Void)Parameters
sessionThe session containing the delayed request.
taskThe task handling the delayed request.
requestThe request that was delayed.
completionHandlerA completion handler to perform the request.
-
sapURLSession(_:taskIsWaitingForConnectivity:)Default implementationTells the delegate that the task is waiting until suitable connectivity is available before beginning the network load. This method is called if the waitsForConnectivity property of URLSessionConfiguration is true, and sufficient connectivity is unavailable. The delegate can use this opportunity to update the user interface; for example, by presenting an offline mode or a cellular-only mode. This method is called, at most, once per task, and only if connectivity is initially unavailable. It is never called for background sessions because waitsForConnectivity is ignored for those sessions.
Default Implementation
Tells the delegate that the task is waiting until suitable connectivity is available before beginning the network load.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, taskIsWaitingForConnectivity task: SAPURLSessionTask)Parameters
sessionThe session that contains the waiting task.
taskThe task that is waiting for a change in connectivity.
-
sapURLSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)Default implementationTells the delegate that the remote server requested an HTTP redirect. This method is called only for tasks in default and ephemeral sessions. Tasks in background sessions automatically follow redirects.
Default Implementation
Tells the delegate that the remote server requested an HTTP redirect.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (SAPURLSession.HTTPRedirectDisposition) -> Swift.Void)Parameters
sessionThe session containing the task whose request resulted in a redirect.
taskThe task whose request resulted in a redirect.
responseAn object containing the server’s response to the original request.
requestA URL request object filled out with the new location.
completionHandlerA completion handler that your delegate method must call.
-
sapURLSession(_:task:didReceive:completionHandler:)Default implementationRequests credentials from the delegate in response to an authentication request from the remote server. This method handles task-level authentication challenges.
Default Implementation
Requests credentials from the delegate in response to an authentication request from the remote server.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (SAPURLSession.AuthChallengeDisposition) -> Swift.Void)Parameters
sessionThe session containing the task whose request requires authentication.
taskThe task whose request requires authentication.
challengeAn object that contains the request for authentication.
completionHandlerA completion handler that your delegate method must call.
-
sapURLSession(_:task:needNewBodyStream:)Default implementationTells the delegate when a task requires a new request body stream to send to the remote server. This delegate method is called under two circumstances:
- To provide the initial request body stream if the task was created with uploadTask(withStreamedRequest:)
To provide a replacement request body stream if the task needs to resend a request that has a body stream because of an authentication challenge or other recoverable server error.
Default Implementation
Tells the delegate when a task requires a new request body stream to send to the remote server.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, needNewBodyStream completionHandler: @escaping (SAPURLSession.InputStreamDisposition) -> Swift.Void)Parameters
sessionThe session containing the task that needs a new body stream.
taskThe task that needs a new body stream.
completionHandlerA completion handler that your delegate method must call.
-
sapURLSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)Default implementationPeriodically informs the delegate of the progress of sending body content to the server.
Default Implementation
Periodically informs the delegate of the progress of sending body content to the server.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)Parameters
sessionThe session containing the data task.
taskThe data task.
bytesSentThe number of bytes sent since the last time this delegate method was called.
totalBytesSentThe total number of bytes sent so far.
totalBytesExpectedToSendThe expected length of the body data.
-
sapURLSession(_:task:didFinishCollecting:)Default implementationTells the delegate that the session finished collecting metrics for the task.
Default Implementation
Tells the delegate that the session finished collecting metrics for the task.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics)Parameters
sessionThe session collecting the metrics.
taskThe task whose metrics have been collected.
metricsThe collected metrics.
-
sapURLSession(_:task:didCompleteWithError:)Default implementationTells the delegate that the task finished transferring data. Server errors are not reported through the error parameter. The only errors your delegate receives through the error parameter are client-side errors, such as being unable to resolve the hostname or connect to the host.
Default Implementation
Tells the delegate that the task finished transferring data.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, didCompleteWithError error: Error?)Parameters
sessionThe session containing the task whose request finished transferring data.
taskThe task whose request finished transferring data.
errorIf an error occurred, an error object indicating how the transfer failed, otherwise nil.
-
sapURLSession(_:dataTask:didReceive:completionHandler:)Default implementationTells the delegate that the data task received the initial reply (headers) from the server. This method is optional unless you need to support the (relatively obscure) multipart/x-mixed-replace content type. With that content type, the server sends a series of parts, each of which is intended to replace the previous part. The session calls this method at the beginning of each part, and you should then display, discard, or otherwise process the previous part, as appropriate. If you do not provide this delegate method, the session always allows the task to continue.
Default Implementation
Tells the delegate that the data task received the initial reply (headers) from the server.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, dataTask: SAPURLSessionTask, didReceive response: URLResponse, completionHandler: @escaping (SAPURLSession.ResponseDisposition) -> Swift.Void)Parameters
sessionThe session containing the data task that received an initial reply.
dataTaskThe data task that received an initial reply.
responseA URL response object populated with headers.
completionHandlerA completion handler that your delegate method must call.
-
sapURLSession(_:dataTask:didBecome:)Default implementationTells the delegate that the data task was changed to a download task.
Default Implementation
Tells the delegate that the data task was changed to a download task.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, dataTask: SAPURLSessionTask, didBecome downloadTask: URLSessionDownloadTask)Parameters
sessionThe session containing the task that was replaced by a download task.
dataTaskThe data task that was replaced by a download task.
downloadTaskThe new download task that replaced the data task.
-
sapURLSession(_:dataTask:didReceive:)Default implementationTells the delegate that the data task has received some of the expected data.
Default Implementation
Tells the delegate that the data task has received some of the expected data.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, dataTask: SAPURLSessionTask, didReceive data: Data)Parameters
sessionThe session containing the data task that provided data.
dataTaskThe data task that provided data.
dataA data object containing the transferred data.
-
sapURLSession(_:dataTask:willCacheResponse:completionHandler:)Default implementationAsks the delegate whether the data (or upload) task should store the response in the cache.
Default Implementation
Asks the delegate whether the data (or upload) task should store the response in the cache.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, dataTask: SAPURLSessionTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @escaping (SAPURLSession.CacheResponseDisposition) -> Swift.Void)Parameters
sessionThe session containing the data (or upload) task.
dataTaskThe data (or upload) task.
proposedResponseThe default caching behavior. This behavior is determined based on the current caching policy and the values of certain received headers, such as the Pragma and Cache-Control headers.
completionHandlerA completion handler that your delegate method must call.
-
sapURLSession(_:downloadTask:didFinishDownloadingTo:)Default implementationTells the delegate that a download task has finished downloading.
Default Implementation
Tells the delegate that a download task has finished downloading.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, downloadTask: SAPURLSessionTask, didFinishDownloadingTo location: URL)Parameters
sessionThe session containing the download task that finished.
downloadTaskThe download task that finished.
locationA file URL for the temporary file. Because the file is temporary, you must either open the file for reading or move it to a permanent location in your app’s sandbox container directory before returning from this delegate method.
-
sapURLSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)Default implementationPeriodically informs the delegate about the download’s progress.
Default Implementation
Periodically informs the delegate about the download’s progress.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, downloadTask: SAPURLSessionTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)Parameters
sessionThe session containing the download task.
downloadTaskThe download task.
bytesWrittenThe number of bytes transferred since the last time this delegate method was called.
totalBytesWrittenThe total number of bytes transferred so far.
totalBytesExpectedToWriteThe expected length of the file, as provided by the Content-Length header. If this header was not provided, the value is NSURLSessionTransferSizeUnknown.
-
sapURLSession(_:downloadTask:didResumeAtOffset:expectedTotalBytes:)Default implementationTells the delegate that the download task has resumed downloading.
Default Implementation
Tells the delegate that the download task has resumed downloading.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, downloadTask: SAPURLSessionTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64)Parameters
sessionThe session containing the download task that finished.
downloadTaskThe download task that resumed. See explanation in the discussion.
fileOffsetIf the file’s cache policy or last modified date prevents reuse of the existing content, then this value is zero. Otherwise, this value is an integer representing the number of bytes on disk that do not need to be retrieved again.
expectedTotalBytesThe expected length of the file, as provided by the Content-Length header. If this header was not provided, the value is NSURLSessionTransferSizeUnknown.
-
sapURLSession(_:task:didCreate:)Default implementationInvoked after request observers are executed, the backing URLSessionDataTask is instantiated.
Default Implementation
Invoked after request observers are executed, the backing URLSessionDataTask is instantiated.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, task: SAPURLSessionTask, didCreate sessionTask: URLSessionTask)Parameters
sessionThe session containing the task which underlying task has been created.
tasksessionTaskThe created underlying session task
-
sapURLSession(_:didCreate:for:)Default implementationInvoked after the resume of a background task, the wrapper SAPURLSessionTask is instantiated.
Default Implementation
Invoked after the resume of a background task, the wrapper SAPURLSessionTask is instantiated.
Declaration
Swift
func sapURLSession(_ session: SAPURLSession, didCreate task: SAPURLSessionTask, for sessionTask: URLSessionTask)Parameters
sessionThe session that came back from background.
taskThe newly created wrapper task.
sessionTaskThe session task that resumed.