Skip to content

Instantly share code, notes, and snippets.

@Alexander-Ignition
Created June 20, 2018 09:34
Show Gist options
  • Save Alexander-Ignition/450c5fb39040729f817d4fb83d9c97d5 to your computer and use it in GitHub Desktop.
Save Alexander-Ignition/450c5fb39040729f817d4fb83d9c97d5 to your computer and use it in GitHub Desktop.
Set additional property to URLRequest
extension URLRequest {
func property(forKey key: String) -> Any? {
return URLProtocol.property(forKey: key, in: self)
}
mutating func setProperty(_ value: Any, forKey key: String) {
mutate { URLProtocol.setProperty(value, forKey: key, in: $0) }
}
mutating func removeProperty(forKey key: String) {
mutate { URLProtocol.removeProperty(forKey: key, in: $0) }
}
private mutating func mutate(_ block: (NSMutableURLRequest) -> Void) {
let mutableURLRequest = self as! NSMutableURLRequest
block(mutableURLRequest)
self = mutableURLRequest as URLRequest
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment