Swiftpack.co - timtwotoes/URLProtocolExtensions as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by timtwotoes.
timtwotoes/URLProtocolExtensions 1.0.0
Making URLProtocol a little bit Swifty
⭐️ 0
🕓 3 weeks ago
.package(url: "https://github.com/timtwotoes/URLProtocolExtensions.git", from: "1.0.0")


Work with URLRequest instead of NSMutableURLRequest when using URLProtocol setProperty and removeProperty class methods.

It seems like this API has some technical debt and haven't been updated since 2016 according to the Swift Core team's repository. While this implementation isn't perfect, as is noted in the Swift Core teams issue, it does make it infinitly easier to work with the API.


Following example implements an additional property bearerToken on URLRequest using URLProtocol property class methods.

extension URLRequest {
    public var bearerToken: String? {
        get {
            return URLProtocol.property(forKey: "BearerToken", in: self) as? String
        set(newValue) {
            if let newBearerToken = newValue {
                URLProtocol.setProperty(newBearerToken, forKey: "BearerToken", in: &self)
            } else {
                URLProtocol.removeProperty(forKey: "BearerToken", in: &self)


Stars: 0
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics