iOS 自建证书不受苹果官方信任,使用wkWebView访问Ht

2018-06-25  本文已影响15人  一如初见丿

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

        }

    }

}

上一篇 下一篇

猜你喜欢

热点阅读