Wkwebview Content Loaded Function Never Get Called

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

}

WKWebView Navigation Delegate Not Called

You need to move let test = TestWebview () to class level, otherwise this object is going to be evicted when viewDidLoad completes: the navigationDelegate is defined as weak, so it's not going to prevent it either.

didFinish delegate not getting called in swiftUI

You need to set the navigation delegate on your WKWebView otherwise it will never be called.

You can do this in the updateUIView call by accessing your coordinator from the context and setting the navigationDelegate to be context.coordinator.

func updateUIView(_ uiView: WKWebView, context: Context) {
guard let myURL = url else {
return
}

uiView.navigationDelegate = context.coordinator // <- This sets the delegate

let request = URLRequest(url: myURL)
uiView.load(request)
}

Once you do that then all navigationDelegate functions should be called if you implement them.

webView didFinish Navigation never gets called

you have to conform to it's protocol delegate by putting this line of code in viewDidLoad

webView?.uiDelegate = self

after doing that , now if you want to use delegation method's you have to put your viewController as a subclass of UIWebViewDelegate

class ViewController: UIViewController ,UIWebViewDelegate{

WKWebView didn't finish loading, when didFinishNavigation is called - Bug in WKWebView?

WKWebView doesn't use delegation to let you know when content loading is complete (that's why you can't find any delegate method that suits your purpose). The way to know whether a WKWebView is still loading is to use KVO (key-value observing) to watch its loading property. In this way, you receive a notification when loading changes from true to false.

Here's a looping animated gif showing what happens when I test this. I load a web view and respond to its loading property through KVO to take a snapshot. The upper view is the web view; the lower (squashed) view is the snapshot. As you can see, the snapshot does capture the loaded content:

Sample Image

[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
if (self->_webKitView.isLoading == true) {
NSLog(@"Still loading...");
}else {
NSLog(@"Finished loading...");
[timer invalidate];
dispatch_async(dispatch_get_main_queue(), ^{
[self->_activityIndicator stopAnimating];
});
}
}];

WKWebView not calling navigation delegate methods

You need to create an instance of ReportsRenderer in ViewController to make it work. When you create an instance in ViewController it lives as long as the ViewController lives. In your case, it was getting deinitialized immediately.

class ViewController: UIViewController {
let reportsRenderer = ReportsRenderer()
// then...
do {
try reportsRenderer.exportPDF(html: string) { (result) in
//...
}


Related Topics



Leave a reply



Submit