【发布时间】:2018-06-10 13:53:12
【问题描述】:
我有一个带有自签名证书的 HTTP REST 服务器。我想使用 alamofire 从 iOS Swift 应用程序与该服务器通信。我目前的代码是:
```
let Almgr : Alamofire.SessionManager = {
// Create the server trust policies
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"localhost": .disableEvaluation
]
// Create custom manager
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
let man = Alamofire.SessionManager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
return man
}()
Almgr.request(url, method: .post, parameters: params).responseJSON {
response in
if response.result.isSuccess {
print("Success")
} else {
print("Failure")
}
}
使用上面的代码 sn-p,当我尝试进行 http 调用 Almgr.request 时总是会出错。错误信息是:
2017-12-30 18:24:20.114486+0530 myApp[58036:2721102] ATS failed system trust
2017-12-30 18:24:20.114625+0530 myApp[58036:2721102] System Trust failed for [1:0x600000178a80]
2017-12-30 18:24:20.114814+0530 myApp[58036:2721102] TIC SSL Trust Error [1:0x600000178a80]: 3:0
2017-12-30 18:24:20.115142+0530 myApp[58036:2721102] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
2017-12-30 18:24:20.115274+0530 myApp[58036:2721102] Task <4E3D9E88-B9CE-48C4-850C-5A3E7C9A6A72>.<1> HTTP load failed (error code: -1200 [3:-9802])
2017-12-30 18:24:20.115469+0530 myApp[58036:2721231] Task <4E3D9E88-B9CE-48C4-850C-5A3E7C9A6A72>.<1> finished with error - code: -1200
知道如何解决这个问题吗?如果 url 是端口 8000 上的 localhost,我不希望进行任何检查。我什至尝试将端口添加到 serverTrustPolicies 定义,但这没有任何区别,我仍然得到错误。有什么帮助吗?
更新:我认为我的问题与https://developer.apple.com/library/content/qa/qa1948/_index.html 有关,但尚未找到解决方法。
【问题讨论】:
-
您是否尝试为
ServerTrustPolicyManager制作自定义类? -
你能详细说明一下吗?我无法理解你的意思。
标签: ios swift ssl https alamofire