An NSURLProtocol object handles the loading of protocol-specific URL data. The NSURLProtocol class itself is an abstract class that provides the infrastructure for processing URLs with a specific URL scheme. You create subclasses for any custom protocols or URL schemes that your app supports.


Apps never need to instantiate an NSURLProtocol subclass directly. When a download starts, the system creates the appropriate protocol object to handle the corresponding URL request. All you have to do is 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, you are encouraged to define categories on NSURLRequest and NSMutableURLRequest with any custom API that you need. Protocol implementors who need to extend the capabilities of NSURLRequest and NSMutableURLRequest in this way can store and retrieve protocol-specific request data by using NSURLProtocol’s class methods property(forKey:in:) and setProperty(_:forKey:in:).

An essential responsibility for a protocol implementor is creating a NSURLResponse for each request it processes successfully. A protocol implementor may wish to create a custom, mutable NSURLResponse class to provide protocol specific information.


Creating Protocol Objects

Registering and Unregistering Protocol Classes

class func registerClass(AnyClass)

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

class func unregisterClass(AnyClass)

Unregisters the specified subclass of NSURLProtocol.

Determining If a Subclass Can Handle a Request

class func canInit(with: URLRequest)

Returns whether the protocol subclass can handle the specified request.

Getting and Setting Request Properties

class func property(forKey: String, in: URLRequest)

Returns 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)

Returns a canonical version of the specified request.

Determining If Requests Are Cache Equivalent

class func requestIsCacheEquivalent(URLRequest, to: URLRequest)

Returns 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 receiver’s cached response.

var client: URLProtocolClient?

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

var request: URLRequest

The receiver’s request.

Instance Properties


Inherits From

Conforms To