我正在使用Alamofire并尝试从我的服务器获取JSON Feed。服务器具有自签名证书和通过用户和密码访问。
这是我的代码
let user = "user"
let password = "password"
let url1 = "https://10.0.1.2:4711/fhem/?cmd=jsonlist2&XHR=1"
let credential = URLCredential(user: user, password: password, persistence: .forSession)
let serverTrustPolicies :[String: ServerTrustPolicy] = [
"10.0.1.8": .disableEvaluation
]
let AlamoSession = SessionManager(serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
AlamoSession.request(url1 )
.authenticate(usingCredential: credential)
.responseJSON { response in
debugPrint(response.response)
print("Result value \(response.result.value)")
//print (response.result.value?.valueForKey("status"))
}
但它确实不起作用!
我该怎么做?
在https Url上获取一个自签名证书的请求?
在我APP的info.plist中添加 应用程序传输安全设置 - 允许任意负载 - 是
答案 0 :(得分:2)
我使用此代码忽略了SSL错误。
import Alamofire
struct WebAPI {
static let sessionManager: SessionManager = {
switch MyEnvironment.server {
case .development:
return Alamofire.SessionManager(
serverTrustPolicyManager: ServerTrustPolicyManagerForDevelop()
)
case .staging, .production:
return Alamofire.SessionManager()
}
}()
private class ServerTrustPolicyManagerForDevelop: ServerTrustPolicyManager {
init() {
super.init(policies: [:])
}
override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
return .disableEvaluation
}
}
}