---
title: URLProtocol
framework: foundation
role: symbol
role_heading: Class
path: foundation/urlprotocol
---

# URLProtocol

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

## Declaration

```swift
class URLProtocol
```

## Overview

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. 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, 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:).

## Topics

### Creating protocol objects

- [init(request:cachedResponse:client:)](foundation/urlprotocol/init(request:cachedresponse:client:).md)
- [init(task:cachedResponse:client:)](foundation/urlprotocol/init(task:cachedresponse:client:).md)

### Registering and unregistering protocol classes

- [registerClass(_:)](foundation/urlprotocol/registerclass(_:).md)
- [unregisterClass(_:)](foundation/urlprotocol/unregisterclass(_:).md)

### Determining If a subclass can handle a request

- [canInit(with:)](foundation/urlprotocol/caninit(with:)-76brg.md)
- [canInit(with:)](foundation/urlprotocol/caninit(with:)-18gbo.md)

### Getting and setting request properties

- [property(forKey:in:)](foundation/urlprotocol/property(forkey:in:).md)
- [setProperty(_:forKey:in:)](foundation/urlprotocol/setproperty(_:forkey:in:).md)
- [removeProperty(forKey:in:)](foundation/urlprotocol/removeproperty(forkey:in:).md)

### Providing a canonical version of a request

- [canonicalRequest(for:)](foundation/urlprotocol/canonicalrequest(for:).md)

### Determining if requests are cache equivalent

- [requestIsCacheEquivalent(_:to:)](foundation/urlprotocol/requestiscacheequivalent(_:to:).md)

### Starting and stopping downloads

- [startLoading()](foundation/urlprotocol/startloading().md)
- [stopLoading()](foundation/urlprotocol/stoploading().md)

### Getting protocol attributes

- [cachedResponse](foundation/urlprotocol/cachedresponse.md)
- [client](foundation/urlprotocol/client.md)
- [URLProtocolClient](foundation/urlprotocolclient.md)
- [request](foundation/urlprotocol/request.md)
- [task](foundation/urlprotocol/task.md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)

## See Also

### Supporting custom protocols

- [protocolClasses](foundation/urlsessionconfiguration/protocolclasses.md)
