Alamofire不受信任的证书。我已经尝试了我能找到的每一个答案

时间:2017-10-31 07:05:10

标签: ios iphone swift

我一直在网上搜索几个小时试图弄清楚为什么这不起作用。

在你投票之前我已经尝试了一切

这是我的info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>https://chargepoints.dft.gov.uk</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
        </dict>

    </dict>
</dict>

这就是我尝试在alamofire上设置会话管理器的方法

private static var Manager: Alamofire.SessionManager = {

    // Create the server trust policies
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "https://chargepoints.dft.gov.uk": .disableEvaluation
    ]

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

    return manager
}()

这是我执行请求的代码

Downloader.Manager.request("https://chargepoints.dft.gov.uk/api/retrieve/registry/format/json").responseJSON { response in
        print("Request: \(String(describing: response.request))")   // original url request
        print("Response: \(String(describing: response.response))") // http url response
        print("Result: \(String(describing: response.result))")                         // response serialization result

        print("Error: \(String(describing: response.error))")

        if let json = response.result.value {
            print("JSON: \(json)") // serialized json response
        }

        if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
            print("Data: \(utf8Text)") // original server data as UTF8 string
        }
    }

哦使用iOS 10.3

XCode 8.3.2

Swift 3.0

提前感谢任何想出这个问题的人!

我要去做一顿油炸早餐,希望有人能帮我解决它!

干杯

2 个答案:

答案 0 :(得分:1)

试试这个

var afManager : SessionManager?

  afManager!.delegate.sessionDidReceiveChallenge = { session, challenge in
        var disposition = URLSession.AuthChallengeDisposition.performDefaultHandling

        var credential : URLCredential?

        if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)
        {
            disposition = URLSession.AuthChallengeDisposition.useCredential
            credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
        }
        else

            if(challenge.previousFailureCount > 0)
            {
                disposition = URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge
            }
            else
            {
                credential = self.afManager!.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
                if(credential != nil)
                {
                    disposition = URLSession.AuthChallengeDisposition.useCredential
                }

        }
        return (disposition, credential)
    }

然后提出您的请求

  afManager?.request("YOUR-URL-HERE", method: .get).responseJSON { response in

            switch response.result {
            case .success:
                print(response.result.value)
                break
            case .failure(let error):
                print(error)
            }
        }

答案 1 :(得分:0)

对于任何想要了解的人,我解决了这个问题,但根据对此问题的评论之一将Https://更改为普通Http

Transport security has blocked a cleartext HTTP

我把头撞在墙上好几天了,但终于得到了一些数据。

然后将域名更改为chargepoints.dft.gov.uk,省略http:或https:最终获得了开始工作的规则。

一切顺利 千斤顶