44

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

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

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

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

}
4

2 回答 2

60

您没有设置 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)");
  }

}

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

}
于 2014-08-26T14:16:39.317 回答
0

我犯了一个简单的错误,没有向我的视图添加子视图

view.addSubview(webView)

我以编程方式创建了一个 webView,这是完整的代码

1..import WebKit

2.. 函数 viewDidLoad()

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

3.. 为委托方法实现扩展

  // MARK: WKWebView
extension 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")
    }
于 2021-05-11T09:39:55.663 回答