WKWebView Content loaded function never get called WKWebView Content loaded function never get called swift swift

WKWebView Content loaded function never get called


You are not setting the navigationDelegate. Set it and it should be fine.

class ViewController: UIViewController, WKNavigationDelegate {  override func viewDidLoad() {    super.viewDidLoad()    let noLayoutFormatOptions = NSLayoutFormatOptions(rawValue: 0)    let webView = WKWebView(frame: CGRectZero, configuration: WKWebViewConfiguration())    webView.setTranslatesAutoresizingMaskIntoConstraints(false)    webView.navigationDelegate = self    view.addSubview(webView)    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: noLayoutFormatOptions, metrics: nil, views: ["webView": webView]))    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: noLayoutFormatOptions, metrics: nil, views: ["webView": webView]))    let url = NSURL(string: "http://google.com")    let request = NSURLRequest(URL: url)    webView.loadRequest(request)  }  func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) {    print("Finished navigating to url \(webView.url)");  }}

And here is a bit better version with Swift 3.

class ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()        let configuration = WKWebViewConfiguration()        let webView = WKWebView(frame: .zero, configuration: configuration)        webView.translatesAutoresizingMaskIntoConstraints = false        webView.navigationDelegate = self        view.addSubview(webView)        [webView.topAnchor.constraint(equalTo: view.topAnchor),         webView.bottomAnchor.constraint(equalTo: view.bottomAnchor),         webView.leftAnchor.constraint(equalTo: view.leftAnchor),         webView.rightAnchor.constraint(equalTo: view.rightAnchor)].forEach  { anchor in            anchor.isActive = true        }        if let url = URL(string: "https://google.com/search?q=westworld") {            webView.load(URLRequest(url: url))        }    }}  extension ViewController: WKNavigationDelegate {    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {        print("Finished navigating to url \(webView.url)")    }}


I made a simple mistake for not adding a subview to my View

view.addSubview(webView)

I have created a webView programmatically, here is the complete code

1.. import WebKit

2.. func viewDidLoad()

let request = URLRequest(url: url)let webView = WKWebView(frame: view.frame)view.addSubview(webView)webView.navigationDelegate = selfwebView.load(request)

3.. Implement extension for delegate methods

  // MARK: WKWebViewextension ViewController: WKNavigationDelegate {        func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {        print("Start Request")    }        func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {        print("Failed Request")    }        func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {        print("Finished Request")    }