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.
SDKs
- iOS 2.0+
- macOS 10.2+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Overview
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 register
class method during your app’s launch time so that the system is aware of your protocol.
Note
You cannot use this class to define custom URL schemes and protocols in watchOS 2 and later.
To support the customization of protocol-specific requests, you are encouraged to define categories on NSURLRequest
and NSMutable
with any custom API that you need. Protocol implementors who need to extend the capabilities of NSURLRequest
and NSMutable
in this way can store and retrieve protocol-specific request data by using NSURLProtocol
’s class methods property(for
and set
.
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.