Fonction WKWebView Content chargée jamais appelée

J'essaye d'get une fonction appelée après que mon contenu dans WKWebView soit complètement chargé. J'ai trouvé la fonction "didFinishNavigation" dans la documentation Apple Swift WKNavigation.

func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) { println("WebView content loaded.") } 

Mais la fonction n'est jamais appelée.

 import UIKit import WebKit class ViewController: UIViewController WKNavigationDelegate { override func loadView() { super.loadView() self.webView = WKWebView(frame:self.containerView.frame, configuration: WKWebViewConfiguration()) self.containerView.addSubview(webView!) self.containerView.clipsToBounds = true } override func viewDidLoad() { super.viewDidLoad() var url = NSURL(ssortingng:"http://google.com/") var req = NSURLRequest(URL:url) self.webView!.loadRequest(req) } func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) { println("WebView content loaded.") } } 

Vous ne définissez pas le paramètre navigationDelegate. Réglez-le et ça devrait aller.

 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, mesortingcs: nil, views: ["webView": webView])) view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: noLayoutFormatOptions, mesortingcs: nil, views: ["webView": webView])) let url = NSURL(ssortingng: "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)"); } } 

Et voici une version un peu mieux avec 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(ssortingng: "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)") } }