iOS 自建证书不受苹果官方信任,使用wkWebView访问Htttps 错误 NSURLErrorDomain Code=-1202 certificate invali

NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “192.168.1.159” which could put your confidential information at risk."

class BaseWebViewController:BaseViewController,WKUIDelegate,WKNavigationDelegate,NSURLConnectionDelegate {

// MARK: - NSURLConnectionDelegate

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

      //后端证书认证

if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {

 #if PRODUCTION

 #else

            //测试环境下 创建凭据对象 信任证书

 let card = URLCredential.init(trust: challenge.protectionSpace.serverTrust!)

            completionHandler(URLSession.AuthChallengeDisposition.useCredential,card)

 #endif


        }

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 有什么可以让我好好的休息 有什么可以让我被庇佑
    雨季和海风阅读 412评论 4 6
  • 月份同河流一起变瘦 时间飞快搓弄 想要拧紧的 两根绳 我们各执一端 时而坠落甜蜜的海 时而在月幻出的弦上 轻舞飞扬...
    杨昊田阅读 504评论 54 54
  • 三月……唉……这大概是我人生当中最低迷的一个春季了…… 一切都是从17号开始。 烦躁 我17号上完舞蹈课后就赶回学...
    Rayanlian阅读 293评论 1 1
  • 有空就去交流会 (文)琼崖红叶 ‘走,赶集上会。’ ‘走。’ 巷里的人逢集市必结伴去购买油...
    囡囡娃阅读 316评论 0 0