1

For UIWebview and WKWebview in iOS, delegate method:

webViewDidFinishLoad:(UIWebView *)webView

and

(void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation

are called when all resources load successfully, however, contents have shown for several seconds before these methods are called.

Now, I want to observe the time when contents show on Webview, anyone who has a good idea please tell me, thanks.

Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
丁小丁
  • 11
  • 3
  • Possible duplicate of [How to detect when a UIWebView has completely finished loading?](http://stackoverflow.com/questions/25445451/how-to-detect-when-a-uiwebview-has-completely-finished-loading) –  Mar 15 '17 at 03:57

3 Answers3

1

You have to use WKWebView to track the loading progress.

First you have to add observers to listen to the progress.

[self.webView addObserver:self forKeyPath:@"loading" options:NSKeyValueObservingOptionNew context:nil];
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];

Then when the web starts loading you handle in this method:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"loading"]) {
        if (!self.webView.isLoading) {
            // Finished loading.
        }

    }
    else if ([keyPath isEqualToString:@"estimatedProgress"]){
          float progress = _webView.estimatedProgress; // Do whatever you want with the progress        
    }
}

In addition, this delegate method gets called when the web view begins loading contents:

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
    // The web starts loading contents. 
}
The Mach System
  • 6,703
  • 3
  • 16
  • 20
-1

Webview did finish load is called when the entire website is loaded.

Your website must have some javascript and css scripts loading after the html is loaded.

Hence the website is displayed, while the javascript is still being loaded.

So the entire website is actually loaded once everything is loaded.

The ideal solution would be to show this to the user. The progress.

Kakshil Shah
  • 3,466
  • 1
  • 17
  • 31
  • Sometimes that takes a lot of time while the content has already been available for some time, which is the problem that 丁小丁 seems to be facing. – Dani Barca Casafont May 10 '18 at 13:35
  • No, I dont think so. The content loads, e.g. html and css. The function is called Then the JS does its work and then the actual content may be displayed. As in for example angular websites. – Kakshil Shah May 21 '18 at 06:02
  • in my experience the JavaScript can start running without this function being called yet. – Dani Barca Casafont May 21 '18 at 09:26
  • ofcourse it can, but in a lot of angular based website, first the entire content is loaded and then the js is called. – Kakshil Shah May 21 '18 at 11:10
-1

I got to solve this very same issue using the delegate didCommitNavigation instead of didFinishNavigation:

- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
    // Your code here
}

At this point seems like the webView is already able to evaluate javascript code. If that's what you're trying to do, you need to know that the site scripts may have not been loaded yet, so the evaluated code must take that into consideration. My personal solution was to use setInterval and keep checking if the function I wanted to call existed.