swiftimport Foundationimport Securityfunc establishSecureConnection() { // 获取证书的句柄 guard let certificate = SecCertificateCreateWithData(nil, certificateData) else { print("无法创建证书") return } // 创建 SSL/TLS 连接上下文 var context = SSLContext() SSLContextCreate(SSLProtocolSide.client, SSLConnectionType.streamType, SSLConnectionType.tlsProtocol1_2, &context) // 配置受信任的根证书 SSLSetCertificateAuthorities(context, [certificate] as CFArray, nil) // 创建安全连接 var connection: SSLConnectionRef? = nil SSLCreateConnection(context, &connection) // 建立连接 SSLSetPeerDomainName(connection, "example.com", "example.com".count) SSLHandshake(connection) // 进行安全通信 // ...}在上述代码中,我们首先使用 `SecCertificateCreateWithData` 函数创建了证书的句柄,然后使用 `SSLContextCreate` 函数创建了 SSL/TLS 连接上下文。接下来,我们使用 `SSLSetCertificateAuthorities` 函数将受信任的根证书配置到连接上下文中。最后,我们使用 `SSLCreateConnection` 函数创建了安全连接,并使用 `SSLHandshake` 函数进行握手。以上就是使用 iPhone TrustStore CA 证书建立安全连接的简单介绍和案例代码。通过使用 TrustStore CA 证书,我们可以确保与服务器之间的通信安全可信,保护用户的隐私和数据安全。