Alamofire 5迁移

问题描述 投票:0回答:1

我在将这段代码迁移到Alamofire 5时遇到问题:

init(plugins: [PluginType] = []) {
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "test.com.ph": .pinCertificates(
            certificates: ServerTrustPolicy.certificates(),
            validateCertificateChain: true,
            validateHost: true
        )
    ]
    let configuration = URLSessionConfiguration.default
    configuration.timeoutIntervalForRequest = 120 // default is 60.
    let manager = Manager(
        configuration: configuration,
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )

    super.init(manager: manager, plugins: plugins)
}

我已经修改了我的代码,但是我不知道如何初始化会话管理器:

let pinEvaluator = PinnedCertificatesTrustEvaluator(certificates: Bundle.main.af.certificates,
                                                    acceptSelfSignedCertificates: true,
                                                    performDefaultValidation: true,
                                                    validateHost: true)

let trustManager = ServerTrustManager.init(evaluators: ["test.com.ph": pinEvaluator])

let sessionManager = Session.init(configuration: .default, serverTrustManager: trustManager)

super.init(session: sessionManager,plugins: plugins)

enter image description here

有人可以帮我吗?

swift alamofire
1个回答
0
投票

由于错误状态,您向未使用ServerTrustEvaluating值注册的主机发出了请求。也就是说,不是test.com.ph的请求。您将要确保向正确的主机发出请求,或者已将正确的主机用于证书固定。

如果希望向多个主机发出请求,则可以通过将ServerTrustManager属性设置为allHostsMustBeEvaluated来初始化false,来禁用此行为。

© www.soinside.com 2019 - 2024. All rights reserved.