UIWebView didFinishLoading fires several times

I have a code that needs to be run after a UIWebView finishes loading the document. To do this, I assigned a UIWebView delegate to my controller and implemented the webViewDidFinishLoading method.

This is caused several times, depending on the type of page being loaded. I'm not sure if this is due to ajax requests, image requests, or even iframes.

Is there a way to say that the main request is complete, which means that the HTML is fully loaded?

Or perhaps delay my code from firing until all these events are complete?

+51
ios objective-c iphone xcode uiwebview
Dec 03 '09 at 19:38
source share
9 answers

This can be enlightening (if you have not gone so far) in NSLog, the boot trace starts and ends.

  - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"Loading: %@", [request URL]); return YES; } - (void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"didFinish: %@; stillLoading: %@", [[webView request]URL], (webView.loading?@"YES":@"NO")); } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"didFail: %@; stillLoading: %@", [[webView request]URL], (webView.loading?@"YES":@"NO")); } 

I just watched the calls to all three in one of my projects, which loads the help page from my package and contains built-in resources (external css, YUI !, images). The only request that comes up is loading the start page, shouldStartLoadWithRequest not called for any of the dependencies. So curious why your didFinishLoad is called multiple times.

Perhaps what you see is related to redirecting or, as already mentioned, ajax calls on the loaded page. But at least you should be able to equalize calls to shouldStartLoad and any of the other two delegate functions and be able to determine when the download will be completed.

+47
Dec 06 '09 at 12:26
source share

You can do something like this to check if the download is complete. Since you can have a lot of content on the same page, you need it.

 - (void)webViewDidFinishLoad:(UIWebView *)webview { if (webview.isLoading) return; // do some work } 
+43
Dec 04 '09 at 13:24
source share

Check it out so simple and simple so that there is no need to write too much code:

 - (void)webViewDidFinishLoad:(UIWebView *)webView { if ([[webView stringByEvaluatingJavaScriptFromString:@"document.readyState"] isEqualToString:@"complete"]) { // UIWebView object has fully loaded. } } 
+11
Apr 23 '14 at 9:52
source share

This question has already been resolved, but I see that it lacks an answer that actually explains why several calls to webViewDidFinishLoad are actually expected behavior

The above method is called every time the web view finishes loading the frame. From the UIWebViewDelegate documentation:

webViewDidFinishLoad:
Dispatched after a webview finishes loading a frame.

In fact, this is also true for all other methods that contain the UIWebViewDelegate protocol.

+7
Mar 24 '13 at 3:19
source share

Try this, it will work fine

  -(void)webViewDidFinishLoad:(UIWebView *)webview { if (webview.isLoading) return; else { // Use the code here which ever you need to run after webview loaded } } 
+4
06 Feb '15 at 11:12
source share

This is because the callback method is called every time a frame is loaded. To prevent this set, the "suppressesIncrementalRendering" property of the web view is true. this will prevent web browsing from being rendered until all data has been loaded into memory. It did the trick for me

+3
Jun 07 '16 at 22:30
source share

I noticed something like this, and that was a mess: I have a UITabBarController , it precedes all ViewControllers associated with its tabs when the application starts (despite displaying only first_Tab_ViewController), so when several tabs have a ViewController with a WebView, their respective webViewDidFinishLoad , and if I copied the paste:

 NSLog(@"size width %0.0f height %0.0f", fitingSize.width, fittingSize.height); 

in several, I get several console outputs that seem to be a double call when they really are a single call in two different UIWebViews .

+2
Aug 25 2018-12-12T00:
source share

You can check the load and request properties in the webViewDidFinishLoad method

+1
Dec 04 '09 at 1:33
source share

Perhaps a related issue is a property in the UIWebView introduced in iOS6: suppressesIncrementalRendering .

0
Mar 25 '13 at 15:59
source share



All Articles