2

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


}
Community
  • 1
  • 1
K.Pan
  • 23
  • 3

1 Answers1

3

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)
}
Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
tsfrank
  • 251
  • 1
  • 2
  • Tried it, works ok with https://google.com, but not self-assigned urls. Got this error: 'fatal error: unexpectedly found nil while unwrapping an Optional value'. – K.Pan Oct 02 '16 at 17:56
  • Found the problem: "challenge.protectionSpace.serverTrust" is nil, got it to work. Thanks – K.Pan Oct 02 '16 at 18:15