Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
738 views
in Technique[技术] by (71.8m points)

ios - WKWebView Content loaded function never get called

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.

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...