Free giveaway: Win a ticket for Do iOS Conference. Learn more.
Free: Win a ticket for Do iOS.
Give your simulator superpowers

RocketSim: An Essential Developer Tool
as recommended by Apple

Printing data requests using a custom URLProtocol

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.

FREE 5-day email course: The Swift Concurrency Playbook by Antoine van der Lee

FREE 5-Day Email Course: The Swift Concurrency Playbook

A FREE 5-day email course revealing the 5 biggest mistakes iOS developers make with with async/await that lead to App Store rejections And migration projects taking months instead of days (even if you've been writing Swift for years)

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.

 
Antoine van der Lee

Written by

Antoine van der Lee

iOS Developer since 2010, former Staff iOS Engineer at WeTransfer and currently full-time Indie Developer & Founder at SwiftLee. Writing a new blog post every week related to Swift, iOS and Xcode. Regular speaker and workshop host.

Are you ready to

Turn your side projects into independence?

Learn my proven steps to transform your passion into profit.