ios WKWebView 内容加载函数永远不会被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25504481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:02:58  来源:igfitidea点击:

WKWebView Content loaded function never get called

iosswiftios8wkwebview

提问by Darx

i try to get a function called after my Content inside WKWebView is fully loaded. I found the "didFinishNavigation" function at the Apple Swift WKNavigation Documentation.

我尝试在 WKWebView 中的内容完全加载后调用一个函数。我在 Apple Swift WKNavigation 文档中找到了“didFinishNavigation”功能。

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

But the function never get called.

但该函数永远不会被调用。

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(string:"http://google.com/")
       var req = NSURLRequest(URL:url)
       self.webView!.loadRequest(req)
   }

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

}

回答by Sandeep

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

您没有设置 navigationDelegate。设置它,它应该没问题。

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.

这里有一个更好的 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)")
    }

}