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()
-
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()
-
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()
-
data(for:Asynchronoususing: ) 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
AnyURLErrorthrown 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
requestThe URL request to perform.
sessionAn optional pre-created pinned session. Pass
nilto create one automatically.Return Value
The raw response data and its
URLResponsemetadata. -
data(from:Asynchronoususing: ) 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
AnyURLErrorthrown 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
urlThe URL to fetch.
sessionAn optional pre-created pinned session. Pass
nilto create one automatically.Return Value
The raw response data and its
URLResponsemetadata. -
download(for:Asynchronoususing: ) 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
AnyURLErrorthrown 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
requestThe URL request to download.
sessionAn optional pre-created pinned session. Pass
nilto create one automatically.Return Value
A tuple of the temporary file URL and the server’s
URLResponse. -
download(from:Asynchronoususing: ) 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
AnyURLErrorthrown 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
urlThe URL to download.
sessionAn optional pre-created pinned session. Pass
nilto create one automatically.Return Value
A tuple of the temporary file URL and the server’s
URLResponse.
-
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 ) -> URLSessionDataTaskParameters
requestThe URL request to perform.
sessionAn optional pre-created pinned session. Pass
nilto create one automatically.completionHandlerCalled when the task finishes, with the response data, the server’s
URLResponse, or an error.Return Value
A suspended
URLSessionDataTask. Callresume()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 ) -> URLSessionDataTaskParameters
urlThe URL to fetch.
sessionAn optional pre-created pinned session. Pass
nilto create one automatically.completionHandlerCalled when the task finishes, with the response data, the server’s
URLResponse, or an error.Return Value
A suspended
URLSessionDataTask. Callresume()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 ) -> URLSessionDownloadTaskParameters
requestThe URL request to download.
sessionAn optional pre-created pinned session. Pass
nilto create one automatically.completionHandlerCalled 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. Callresume()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 ) -> URLSessionDownloadTaskParameters
urlThe URL to download.
sessionAn optional pre-created pinned session. Pass
nilto create one automatically.completionHandlerCalled 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. Callresume()to start it.
View on GitHub
Install in Dash