Swift 使用 TLS 的 NWConnection 失败

NWConnection using TLS fails in Swift

我能够像这样使用 tls 连接到 public wss:

let port = 443
let url = URL(string: "wss://echo.websocket.org")!
connection = NWConnection(host: NWEndpoint.Host.name(url.host!, nil), port: NWEndpoint.Port(rawValue: UInt16(port))!, using: .tls)

现在我正在尝试连接到专用网络中的 wss websocket,但出现了一些错误。这是我的代码:

let port = 8443
let url = URL(string: "wss://ip_address:8443/gs-guide-websocket/websocket")!
connection = NWConnection(host: NWEndpoint.Host.name(url.host!, nil), port: NWEndpoint.Port(rawValue: UInt16(port))!, using: .tls)

这些是错误:

[BoringSSL] boringssl_context_alert_callback_handler(3747) [C1.1:1][0x7fe945708390] Alert level: fatal, description: certificate unknown
[BoringSSL] boringssl_context_error_print(3699) boringssl ctx 0x600001ec9500: 140639868076392:error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED:/BuildRoot/Library/Caches/com.apple.xbs/Sources/boringssl_Sim/boringssl-109.202.1/ssl/handshake.cc:372:
[BoringSSL] boringssl_context_get_error_code(3519) [C1.1:1][0x7fe945708390] SSL_AD_CERTIFICATE_UNKNOWN

Client failed with error: -9808: Optional(bad certificate format)

我需要添加特殊选项才能在此处连接吗?

此外,我看到 wss 工作正常,因为我能够连接 SocketRocket,但我想连接 NWConnection

谢谢。

解决方案是安装我在服务器上拥有的证书,然后在“设置”>“通用”>“关于”>“证书信任设置”中启用对该证书的完全信任