TrustPinURLProtocol

@available(iOS 13.0, macOS 13.0, tvOS 13.0, watchOS 7.0, visionOS 2.0, *)
public final class TrustPinURLProtocol : URLProtocol, @unchecked Sendable

A URLProtocol implementation that performs certificate pinning using TrustPin verification.

This protocol intercepts HTTPS requests and validates server certificates against domain-specific whitelists using the TrustPin verification system.

Usage

// Register the protocol
TrustPinURLProtocol.register()

// Use with URLSession
let session = URLSession(configuration: .default)
// Requests will automatically use certificate pinning

// Unregister when no longer needed
TrustPinURLProtocol.unregister()

Registration

  • Registers the TrustPinURLProtocol with the URL loading system.

    Declaration

    Swift

    public static func register()
  • Unregisters the TrustPinURLProtocol from the URL loading system.

    Declaration

    Swift

    public static func unregister()

URLProtocol Methods

  • Determines whether this protocol can handle the given request.

    Declaration

    Swift

    public override static func canInit(with request: URLRequest) -> Bool
  • Returns a canonical version of the given request.

    Declaration

    Swift

    public override static func canonicalRequest(for request: URLRequest) -> URLRequest
  • Starts loading the request.

    Declaration

    Swift

    public override func startLoading()
  • Stops loading the request.

    Declaration

    Swift

    public override func stopLoading()

Async/Await Support

  • data(for:using:) Asynchronous

    Fetches the contents of a URL request using certificate pinning (async/await).

    Equivalent to URLSession.data(for:) but with TrustPin certificate pinning applied to every HTTPS connection. If no session is supplied, a new pinned session is created for each call.

    Throws

    Any URLError thrown by the underlying URLSession, or a TrustPin certificate pinning error if validation fails.

    Declaration

    Swift

    static func data(
        for request: URLRequest,
        using session: URLSession? = nil
    ) async throws -> (Data, URLResponse)

    Parameters

    request

    The URL request to perform.

    session

    An optional pre-created pinned session. Pass nil to create one automatically.

    Return Value

    The raw response data and its URLResponse metadata.

  • data(from:using:) Asynchronous

    Fetches the contents of a URL using certificate pinning (async/await).

    Equivalent to URLSession.data(from:) but with TrustPin certificate pinning applied to every HTTPS connection. If no session is supplied, a new pinned session is created for each call.

    Throws

    Any URLError thrown by the underlying URLSession, or a TrustPin certificate pinning error if validation fails.

    Declaration

    Swift

    static func data(
        from url: URL,
        using session: URLSession? = nil
    ) async throws -> (Data, URLResponse)

    Parameters

    url

    The URL to fetch.

    session

    An optional pre-created pinned session. Pass nil to create one automatically.

    Return Value

    The raw response data and its URLResponse metadata.

  • download(for:using:) Asynchronous

    Downloads a file for a URL request using certificate pinning (async/await).

    Equivalent to URLSession.download(for:) but with TrustPin certificate pinning applied. On iOS 15+ and macOS 12+ the native async download API is used; on earlier OS versions a continuation-based fallback is used transparently.

    Throws

    Any URLError thrown by the underlying URLSession, or a TrustPin certificate pinning error if validation fails.

    Declaration

    Swift

    static func download(
        for request: URLRequest,
        using session: URLSession? = nil
    ) async throws -> (URL, URLResponse)

    Parameters

    request

    The URL request to download.

    session

    An optional pre-created pinned session. Pass nil to create one automatically.

    Return Value

    A tuple of the temporary file URL and the server’s URLResponse.

  • download(from:using:) Asynchronous

    Downloads a file from a URL using certificate pinning (async/await).

    Equivalent to URLSession.download(from:) but with TrustPin certificate pinning applied. On iOS 15+ and macOS 12+ the native async download API is used; on earlier OS versions a continuation-based fallback is used transparently.

    Throws

    Any URLError thrown by the underlying URLSession, or a TrustPin certificate pinning error if validation fails.

    Declaration

    Swift

    static func download(
        from url: URL,
        using session: URLSession? = nil
    ) async throws -> (URL, URLResponse)

    Parameters

    url

    The URL to download.

    session

    An optional pre-created pinned session. Pass nil to create one automatically.

    Return Value

    A tuple of the temporary file URL and the server’s URLResponse.

Completion Handler Support

  • Creates a data task that performs certificate pinning using a completion handler.

    Declaration

    Swift

    static func dataTask(
        with request: URLRequest,
        using session: URLSession? = nil,
        completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void
    ) -> URLSessionDataTask

    Parameters

    request

    The URL request to perform.

    session

    An optional pre-created pinned session. Pass nil to create one automatically.

    completionHandler

    Called when the task finishes, with the response data, the server’s URLResponse, or an error.

    Return Value

    A suspended URLSessionDataTask. Call resume() to start it.

  • Creates a data task that performs certificate pinning using a completion handler.

    Declaration

    Swift

    static func dataTask(
        with url: URL,
        using session: URLSession? = nil,
        completionHandler: @escaping @Sendable (Data?, URLResponse?, Error?) -> Void
    ) -> URLSessionDataTask

    Parameters

    url

    The URL to fetch.

    session

    An optional pre-created pinned session. Pass nil to create one automatically.

    completionHandler

    Called when the task finishes, with the response data, the server’s URLResponse, or an error.

    Return Value

    A suspended URLSessionDataTask. Call resume() to start it.

  • Creates a download task that performs certificate pinning using a completion handler.

    Declaration

    Swift

    static func downloadTask(
        with request: URLRequest,
        using session: URLSession? = nil,
        completionHandler: @escaping @Sendable (URL?, URLResponse?, Error?) -> Void
    ) -> URLSessionDownloadTask

    Parameters

    request

    The URL request to download.

    session

    An optional pre-created pinned session. Pass nil to create one automatically.

    completionHandler

    Called when the download finishes, with the temporary file URL, the server’s URLResponse, or an error. Move the file before the handler returns.

    Return Value

    A suspended URLSessionDownloadTask. Call resume() to start it.

  • Creates a download task that performs certificate pinning using a completion handler.

    Declaration

    Swift

    static func downloadTask(
        with url: URL,
        using session: URLSession? = nil,
        completionHandler: @escaping @Sendable (URL?, URLResponse?, Error?) -> Void
    ) -> URLSessionDownloadTask

    Parameters

    url

    The URL to download.

    session

    An optional pre-created pinned session. Pass nil to create one automatically.

    completionHandler

    Called when the download finishes, with the temporary file URL, the server’s URLResponse, or an error. Move the file before the handler returns.

    Return Value

    A suspended URLSessionDownloadTask. Call resume() to start it.