Class

URLProtocol

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

Declaration

class URLProtocol : NSObject

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:

Topics

Creating Protocol Objects

Registering and Unregistering Protocol Classes

class func registerClass(AnyClass) -> Bool

Attempts to register a subclass of URLProtocol, making it visible to the URL loading system.

class func unregisterClass(AnyClass)

Unregisters the specified subclass of URLProtocol.

Determining If a Subclass Can Handle a Request

class func canInit(with: URLRequest) -> Bool

Determines whether the protocol subclass can handle the specified request.

class func canInit(with: URLSessionTask) -> Bool

Determines whether the protocol subclass can handle the specified task.

Getting and Setting Request Properties

class func property(forKey: String, in: URLRequest) -> Any?

Fetches the property associated with the specified key in the specified request.

class func setProperty(Any, forKey: String, in: NSMutableURLRequest)

Sets the property associated with the specified key in the specified request.

class func removeProperty(forKey: String, in: NSMutableURLRequest)

Removes the property associated with the specified key in the specified request.

Providing a Canonical Version of a Request

class func canonicalRequest(for: URLRequest) -> URLRequest

Returns a canonical version of the specified request.

Determining If Requests Are Cache Equivalent

class func requestIsCacheEquivalent(URLRequest, to: URLRequest) -> Bool

A Boolean value indicating whether two requests are equivalent for cache purposes.

Starting and Stopping Downloads

func startLoading()

Starts protocol-specific loading of the request.

func stopLoading()

Stops protocol-specific loading of the request.

Getting Protocol Attributes

var cachedResponse: CachedURLResponse?

The protocol’s cached response.

var client: URLProtocolClient?

The object the protocol uses to communicate with the URL loading system.

protocol URLProtocolClient

The interface used by URLProtocol subclasses to communicate with the URL Loading System.

var request: URLRequest

The protocol’s request.

var task: URLSessionTask?

The protocol’s task.

Relationships

Inherits From

Conforms To

See Also

Supporting Custom Protocols

var protocolClasses: [AnyClass]?

An array of extra protocol subclasses that handle requests in a session.

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