iOS swift HTTPS 9813错误解决记录

来源:互联网 发布:jsp企业门户网站源码 编辑:程序博客网 时间:2024/06/05 05:11

说明:只是测试版解决了,即未发布的。后面发布时遇到的问题,会再次记录。


环境是:

Alamofire 4.0 + swift 3.0 + WKWebView + xcode 8.1(8B62) + OS X EI Capitan 10.11.6


解决时间:

2017-08-15


针对问题:

(NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813))


主要解决功能:

1、是alamofire swift3.0 设置授信ssl访问数据接口,返回json;

2、是web加载https 网页的;

3、加载http&&加载HTTPS都启用


第一种(alamofire swift3.0 设置授信ssl访问数据接口,返回json):

1、配置info.plist


swift 版的配置

2、(Alamofire 4.0)在Alamofire.request 之前设置验证通过


swift 3.0版代码(Alamofire 4.0)

let manager = SessionManager.default

manager.delegate.sessionDidReceiveChallenge = { session, challenge in

//认证服务器证书

if challenge.protectionSpace.authenticationMethod

== NSURLAuthenticationMethodServerTrust {

print("服务端证书认证!")

return (URLSession.AuthChallengeDisposition.useCredential,

URLCredential(trust: challenge.protectionSpace.serverTrust!))

}

//认证客户端证书

else if challenge.protectionSpace.authenticationMethod

== NSURLAuthenticationMethodClientCertificate {

print("客户端证书认证!")

return (.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!));

}

// 其它情况(不接受认证)

else {

print("其它情况(不接受认证)")

return (.cancelAuthenticationChallenge, nil)

}

}


第二种,网页加载:

1、delete是WKNavigationDelegate

webViews.navigationDelegate = weakSelf!

2、https验证的控制方法是


swift 3.0版代码 WKWebView

- End -


喜欢请点亮小红心,热爱请关注,打赏请随意^_^。

您留下的每一个足迹都是我前进的动力。

谢谢您的到来,感谢您的阅读。