如何信任端点 iOS swift

How to trust an endpoint iOS swift

我正在调用具有自签名 ssl 证书的端点我已尝试将其添加到我的 info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

但我仍然无法访问端点我一直收到这个

NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “endpoint” which could put your confidential information at risk.

您需要创建一个会话管理器并告诉它禁用对该服务器中的 ssl 的评估。

像这样

static var manager: Alamofire.SessionManager = {
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "https://example.com": .disableEvaluation
    ]

    let configuration = URLSessionConfiguration.default
    configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
    let manager = Alamofire.SessionManager(
        configuration: URLSessionConfiguration.default,
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )

    return manager
}()

然后,而不是像这样在 Alamofire 中调用请求

Alamofire.request("https://example.com", method: .get…

你在你的经理中称呼它

manager.request("https://example.com"…