Contents

URLProtocol

An abstract class that handles the loading of protocol-specific URL data.

Declaration

class URLProtocol

Overview

Don’t instantiate a URLProtocol subclass directly. Instead, create subclasses for any custom protocols or URL schemes that your app supports. When a download starts, the system creates the appropriate protocol object to handle the corresponding URL request. You define your protocol class and call the registerClass(_:) class method during your app’s launch time so that the system is aware of your protocol.

To support the customization of protocol-specific requests, create extensions to the URLRequest class to provide any custom API that you need. You can store and retrieve protocol-specific request data by using URLProtocol’s class methods property(forKey:in:) and setProperty(_:forKey:in:).

Create a URLResponse for each request your subclass processes successfully. You may want to create a custom URLResponse class to provide protocol specific information.

Subclassing notes

When overriding methods of this class, be aware that methods that take a task parameter are preferred by the system to those that do not. Therefore, you should override the task-based methods when subclassing, as follows:

Swift:

Objective-C:

Topics

Creating protocol objects

Registering and unregistering protocol classes

Determining If a subclass can handle a request

Getting and setting request properties

Providing a canonical version of a request

Determining if requests are cache equivalent

Starting and stopping downloads

Getting protocol attributes

See Also

Supporting custom protocols