Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone: How do I know the UIWebView is fully loaded?

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

like image 885
Jackson Tale Avatar asked May 21 '11 13:05

Jackson Tale


2 Answers

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"]];
}
like image 118
Stanislav S. Avatar answered Sep 25 '22 17:09

Stanislav S.


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.

like image 20
Roki Avatar answered Sep 21 '22 17:09

Roki