TrustPin
public final class TrustPin : @unchecked Sendable
SSL certificate pinning for iOS, macOS, tvOS, watchOS, and visionOS.
Single instance (app — existing usage, zero changes required)
try await TrustPin.setup(TrustPinConfiguration(
organizationId: "org", projectId: "proj", publicKey: key
))
try await TrustPin.verify(domain: "api.example.com", certificate: pem)
Multiple instances (library or multi-tenant app)
A library should create its own isolated instance so it never interferes with the host app’s pinning configuration:
// Inside the library (keep this reference private/internal)
let pin = TrustPin.instance(id: "com.mylib.networking")
try await pin.setup(TrustPinConfiguration(
organizationId: "lib-org", projectId: "lib-proj", publicKey: libKey
))
try await pin.verify(domain: "api.library.com", certificate: pem)
URLProtocol (default instance only)
registerURLProtocol() and unregisterURLProtocol() are static methods
and always operate on TrustPin.default. They are intentionally unavailable
on named instances — libraries should use makeURLSessionDelegate() instead.
Thread Safety
All operations are thread-safe. Internal state is protected by Swift actors.
-
The default
TrustPininstance.All static convenience methods (
TrustPin.setup(...),TrustPin.verify(...), etc.) delegate to this instance. Existing callers that use the static API never need to reference this property directly.Declaration
Swift
public static let `default`: TrustPin
-
Returns the
TrustPininstance registered underid, creating it if needed.Calls with the same
idalways return the same object — the registry is process-global and thread-safe. Use a reverse-DNS string (e.g."com.mylib") to avoid collisions with other libraries.Note
URLProtocolregistration is not available on named instances. UsemakeURLSessionDelegate()for library-scoped pinning.Declaration
Swift
public static func instance(id: String) -> TrustPinParameters
idA stable, unique identifier for this pinning context. Must not be
"default"— usedefaultinstead.Return Value
The existing or newly created
TrustPininstance forid.
-
setup(_:Asynchronous) Configures this TrustPin context with the given credentials and options.
Throws
invalidProjectConfigif credentials are missing or invalid.Throws
errorFetchingPinningInfoif the pinning payload cannot be fetched.Throws
configurationValidationFailedif signature verification fails.Declaration
Swift
public func setup(_ configuration: TrustPinConfiguration) async throwsParameters
configurationA
TrustPinConfigurationvalue with your project credentials and optional settings (mode, log level, custom URL). -
verify(domain:Asynchronouscertificate: ) Verifies a PEM certificate against the configured pins for
domain.Throws
invalidProjectConfigifsetup(_:)has not been called.Throws
domainNotRegisteredif domain is not in the payload (strict mode).Throws
pinsMismatchif no configured pin matches the certificate.Throws
allPinsExpiredif every pin for the domain has expired.Throws
invalidServerCertif the certificate cannot be parsed.Declaration
Swift
public func verify(domain: String, certificate: String) async throwsParameters
domainThe hostname to validate (e.g.
"api.example.com").certificatePEM-encoded certificate string including
BEGIN/ENDmarkers. -
fetchCertificate(host:Asynchronousport: ) Fetches the TLS leaf certificate from
host:portas a PEM string.Opens an ephemeral side-channel TLS connection, performs OS-level chain validation, extracts the leaf certificate, and immediately cancels the connection without sending any HTTP data.
Throws
invalidServerCertif the TLS handshake fails.Declaration
Swift
public func fetchCertificate(host: String, port: Int = 443) async throws -> StringParameters
hostHostname to connect to (e.g.
"api.example.com").portTCP port (default:
443).Return Value
PEM-encoded leaf certificate string.
-
Sets the log level for this TrustPin instance.
Declaration
Swift
public func set(logLevel: TrustPinLogLevel)Parameters
levelThe desired
TrustPinLogLevel. -
Returns a
URLSessionDelegatebound to this instance that performs certificate pinning.Use this for library-scoped pinning instead of the global URLProtocol:
let session = URLSession( configuration: .default, delegate: pin.makeURLSessionDelegate(), delegateQueue: nil )Declaration
Swift
public func makeURLSessionDelegate() -> any URLSessionDelegate -
Returns a
URLSessionDelegatebound to the defaultTrustPininstance.Equivalent to
TrustPin.default.makeURLSessionDelegate().let session = URLSession( configuration: .default, delegate: TrustPin.makeURLSessionDelegate(), delegateQueue: nil )Declaration
Swift
public static func makeURLSessionDelegate() -> any URLSessionDelegate
-
setup(_:AsynchronousautoRegisterURLProtocol: ) Configures the default TrustPin instance.
Equivalent to
TrustPin.default.setup(configuration).Throws
invalidProjectConfigif credentials are missing or invalid.Throws
errorFetchingPinningInfoif the pinning payload cannot be fetched.Throws
configurationValidationFailedif signature verification fails.Declaration
Swift
public static func setup(_ configuration: TrustPinConfiguration, autoRegisterURLProtocol: Bool = false) async throwsParameters
configurationA
TrustPinConfigurationvalue with your project credentials and optional settings (mode, custom URL).autoRegisterURLProtocolWhen
true, automatically registersTrustPinURLProtocolfor system-wide pinning after setup. Requires iOS 13+ / macOS 13+. Defaults tofalse. -
verify(domain:Asynchronouscertificate: ) Verifies a certificate using the default TrustPin instance.
Equivalent to
TrustPin.default.verify(domain:certificate:).Declaration
Swift
public static func verify(domain: String, certificate: String) async throws -
fetchCertificate(host:Asynchronousport: ) Fetches a certificate using the default TrustPin instance.
Equivalent to
TrustPin.default.fetchCertificate(host:port:).Declaration
Swift
public static func fetchCertificate(host: String, port: Int = 443) async throws -> String -
Sets the log level on the default TrustPin instance.
Equivalent to
TrustPin.default.set(logLevel:).Declaration
Swift
public static func set(logLevel: TrustPinLogLevel)
-
Registers the TrustPin URLProtocol for system-wide pinning using the default instance.
Note
Only available on the default instance. Libraries should usemakeURLSessionDelegate()instead.Declaration
Swift
@available(iOS 13.0, macOS 13.0, tvOS 13.0, watchOS 7.0, visionOS 2.0, *) public static func registerURLProtocol() -
Unregisters the TrustPin URLProtocol.
Declaration
Swift
@available(iOS 13.0, macOS 13.0, tvOS 13.0, watchOS 7.0, visionOS 2.0, *) public static func unregisterURLProtocol()
View on GitHub
Install in Dash