Tells the delegate that the data task received the initial reply (headers) from the server.
SDKs
- iOS 7.0+
- macOS 10.9+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
optional func urlSession(_ session: URLSession, dataTask: URLSession Data Task, didReceive response: URLResponse, completionHandler: @escaping (URLSession.Response Disposition) -> Void)
Parameters
session
The session containing the data task that received an initial reply.
dataTask
The data task that received an initial reply.
response
A URL response object populated with headers.
completionHandler
A completion handler that your code calls to continue a transfer, passing a
URLSession
constant to indicate whether the transfer should continue as a data task or should become a download task..Response Disposition If you pass
URLSession
, the task continues as a data task..Response Disposition .allow If you pass
URLSession
, the task is canceled..Response Disposition .cancel If you pass
URLSession
, your delegate’s.Response Disposition .become Download url
method is called to provide the new download task that supersedes the current task.Session(_: data Task: did Become:)
Discussion
Implementing this method is optional unless you need to cancel the transfer or convert it to a download task when the response headers are first received. If you don’t provide this delegate method, the session always allows the task to continue.
You also implement this method if you need to support the fairly 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, followed by one or more calls to url
with the contents of that part.
Each time the url
method is called for a part, collect the data received for the previous part (if any) and process the data as needed for your application. This processing can include storing the data to the filesystem, parsing it into custom types, or displaying it to the user. Next, begin receiving the next part by calling the completion handler with the URLSession
constant. Finally, if you have also implemented url
, the session will call it after sending all the data for the last part.