K.Pan
K.Pan

Reputation: 23

IOS wkwebview using self-assigned Cert

Trying to load a HTTPS Url with a self assigned cert for IOS 10 and keep failing.

Found similar problem in this thread, but the solution is not suitable for the newer version of Swift / SDK / IOS : Allow unverified ssl certificates in WKWebView

I am not looking for a solution that is valid for App Store, just need to be able to ignore the SSL certification validation.

Here is the ViewController.swift file:

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)
    }


}

Upvotes: 2

Views: 2740

Answers (1)

tsfrank
tsfrank

Reputation: 261

Your delegate method signature doesn't quite match. Try this:

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

Upvotes: 3

Related Questions