A protocol that most delegates of a URL connection implement to receive data associated with the connection.


protocol NSURLConnectionDataDelegate


The NSURLConnectionDataDelegate protocol describes methods that should be implemented by the delegate for an instance of the NSURLConnection class. Many methods in this protocol existed as part of an informal protocol in previous versions of macOS and iOS.

In addition to the methods described in this protocol, an NSURLConnection delegate should also implement the methods described in the NSURLConnectionDelegate protocol.


Handling Incoming Data

func connection(NSURLConnection, didReceive: URLResponse)

Sent when the connection has received sufficient data to construct the URL response for its request.

func connection(NSURLConnection, didReceive: Data)

Sent as a connection loads data incrementally.

Receiving Connection Progress

func connection(NSURLConnection, didSendBodyData: Int, totalBytesWritten: Int, totalBytesExpectedToWrite: Int)

Sent as the body (message data) of a request is transmitted (such as in an HTTP POST request).

func connectionDidFinishLoading(NSURLConnection)

Sent when a connection has finished loading successfully.

Handling Redirects

func connection(NSURLConnection, willSend: URLRequest, redirectResponse: URLResponse?) -> URLRequest?

Sent when the connection determines that it must change URLs in order to continue loading a request.

func connection(NSURLConnection, needNewBodyStream: URLRequest) -> InputStream?

Called when an NSURLConnection needs to retransmit a request that has a body stream to provide a new, unopened stream.

Overriding Caching Behavior

func connection(NSURLConnection, willCacheResponse: CachedURLResponse) -> CachedURLResponse?

Sent before the connection stores a cached response in the cache, to give the delegate an opportunity to alter it.


See Also

URL Connection

class NSURLConnection

An object that enables you to start and stop URL requests.

protocol NSURLConnectionDelegate

A protocol that delegates of a URL connection implement to receive status about and provide feedback to the connection object.

protocol NSURLConnectionDownloadDelegate

A protocol that delegates of a URL connection created with Newsstand Kit implement to receive data associated with a download.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software