IOS wkwebview使用自我分配的证书

时间:2016-10-02 16:28:07

标签: ios swift ssl wkwebview

尝试为IOS 10加载具有自我分配证书的HTTPS Url并继续失败。

在此主题中发现了类似的问题,但该解决方案不适用于较新版本的Swift / SDK / IOS:Allow unverified ssl certificates in WKWebView

我不是在寻找对App Store有效的解决方案,只需要能够忽略SSL认证验证。

这是ViewController.swift文件:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

    var webView: WKWebView!

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self

        view = webView
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let url = URL(string: "https://X.X.X.X:XXXX")!
        //let url = URL(string: "https://google.com")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func webView(webView: WKWebView, didReceiveAuthenticationChallenge challenge: URLAuthenticationChallenge,
             completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        let cred = URLCredential.init(trust: challenge.protectionSpace.serverTrust!)
        completionHandler(.useCredential, cred)
    }


}

1 个答案:

答案 0 :(得分:1)

您的委托方法签名不完全匹配。试试这个:

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    let cred = URLCredential.init(trust: challenge.protectionSpace.serverTrust!)
    completionHandler(.useCredential, cred)
}