Almost all apps contain some kind of data requests. Printing data requests could sometimes be handy for debugging purposes. This can be done fairly easy by using a custom URLProtocol
.
Creating a custom URLProtocol
A custom URLProtocol
is needed to print out the data requests. A custom implementation of URLProtocol
including the canInit
method is enough for this case.
/// A custom protocol for logging outgoing requests.
final class PrintProtocol: URLProtocol {
override open class func canInit(with request: URLRequest) -> Bool {
// Print valuable request information.
print("? Running request: \(request.httpMethod ?? "") - \(request.url?.absoluteString ?? "")")
// By returning `false`, this URLProtocol will do nothing less than logging.
return false
}
}
The protocol will mostly be ignored as we’re returning false
. It’s only used for printing and will not affect outgoing requests.
Enabling the protocol
The protocol needs to be enabled before it starts working. This can be done with a simple line of code:
// Register the custom URL Protocol.
URLProtocol.registerClass(PrintProtocol.self)
Although this is often enough to make it work, it’s sometimes needed to create your own custom session configuration. This also counts for usage with Alamofire.
let configuration = URLSessionConfiguration.default
configuration.protocolClasses?.insert(PrintProtocol.self, at: 0)
let sessionManager = Alamofire.SessionManager(configuration: configuration)
sessionManager.request("https://www.avanderlee.com/feed/")
For more information about advanced usage with Alamofire, checkout their documentation.
Try it out yourself
? Running request: GET - https://www.avanderlee.com/feed/
✅ Request completed
Using this playground code gives you the above output:
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
/// A custom protocol for logging outgoing requests.
final class PrintProtocol: URLProtocol {
override open class func canInit(with request: URLRequest) -> Bool {
// Print valuable request information.
print("? Running request: \(request.httpMethod ?? "") - \(request.url?.absoluteString ?? "")")
// By returning `false`, this URLProtocol will do nothing less than logging.
return false
}
}
// Register the custom URL Protocol.
URLProtocol.registerClass(PrintProtocol.self)
// Execute a data request.
URLSession.shared.dataTask(with: URL(string: "https://www.avanderlee.com/feed/")!) { (_, _, _) in
print("✅ Request completed")
}.resume()
An advanced usage of a custom URLProtocol
This is just a simple example of what you can do with a custom URLProtocol
. More advanced implementations enable you to for example mock your unit tests. You can checkout WeTransfer’s Mocker framework for some inspiration.