URLProtocol
An abstract class that handles the loading of protocol-specific URL data.
Declaration
class URLProtocolOverview
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:
Initialization — Override init(task:cachedResponse:client:) instead of or in addition to init(request:cachedResponse:client:). Also override the task-based canInit(with:) instead of or in addition to the request-based canInit(with:).
Objective-C:
Initialization — Override canInit(with:) and init(task:cachedResponse:client:) instead of or in addition to canInit(with:) and init(request:cachedResponse:client:).