7

Yes, I know UIWebView has didFinishedLoad & didStartLoad delegate.

However, the didFinishedLoad does not mean the full completion. It may be called when one of the items that the UIWebView is finished loading. i.e., UIWebView may call this delegate several times while loading a single page.

So anyway can tell me how to check whether the UIWebView is fully loaded?

Thanks

J

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
  • Have you done that http://stackoverflow.com/questions/1842370/uiwebview-didfinishloading-fires-multiple-times first?? – phi May 21 '11 at 13:31

3 Answers3

11

I have poor experience with DOM but after some searching I found that the document.readyState is the great option.

From w3schools:

Definition and Usage The readyState property returns the (loading) status of the current document.

This property returns one of four values:

uninitialized - Has not started loading yet

loading - Is loading

interactive - Has loaded enough and the user can interact with it

complete - Fully loaded

So I'm using this to know when UIWebView has loaded the document:

- (void)readyState:(NSString *)str
{ NSLog(@"str:%@",str);

if ([str isEqualToString:@"complete"]||[str isEqualToString:@"interactive"]) {
    NSLog(@"IT HAS BEEN DONE");
    [pageLoadingActivityIndicator stopAnimating];
}

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
 //other code...
 [self readyState:[browserWebView stringByEvaluatingJavaScriptFromString:@"document.readyState"]];
}
Stanislav S.
  • 253
  • 3
  • 6
  • if ([str isEqualToString:@"complete"]||[str isEqualToString:@"interactive"]) is the only thing that worked for me! – Mohamed A.Karim Jul 23 '13 at 22:09
  • 1
    I got "IT HAS BEEN DONE" log 27 times from just one web page. So i cant say it is working as i expect – Yucel Bayram Apr 01 '14 at 08:12
  • @yucel bayram when webpage has iframes or other kind of internal things that are, in fact, a webpage that is loading simultaneously with the requested webpage, then the "complete" message will be delivered more than once. Also there can be other thing that modify 'document.readyState'. This approach works well for simple webpages. – Stanislav S. Nov 18 '16 at 15:24
  • Thanks @StanislavS. – Yucel Bayram Nov 21 '16 at 06:35
6

http://www.codingventures.com/2008/12/using-uiwebview-to-render-svg-files/ on Javascript communicating back with Objective-C code

Maybe use document location hash.

And add in the webview html body:

<body onload="document.location.hash='myapp:myobject:myfunction';">

I know its a little bit hacky but works. And it can be used in ajax based contents, because its up to you when you want to call your ready method. Or it can be used as a complete communication scheme.

Roki
  • 2,680
  • 1
  • 22
  • 21
0

What you can do is, display a network indicator on the view which is visible in the status bar. When your page is being loaded the indicator will continue rotating until and unless the page is completly loaded. When the page completely loads, the indicator will stop rotating and will be invisible.
Just have a look at this example : Network indicator

Nitish
  • 13,845
  • 28
  • 135
  • 263