Dan Ray
Dan Ray

Reputation: 21903

UIWebView finishes too fast

I'm loading a biggish (and javascript-heavy) page into a UIWebView, and I want to add a UIActivityView to spin while it thinks.

Problem is, my - (void)webViewDidFinishLoad:(UIWebView *)webView method gets called quite a while before the rendering actually happens. Enough so that my spinner (which set to hide when stopped) never actually shows up. By the time the UI is even assembled, the spinner has already been stopped and hidden, even though there's enough time to wonder if it's broken before the UIWebView actually gets the goods to the screen.

I wish there was a "webViewDidFinishRendering", but that would imply that WebKit is something other than lickedy-split fast... ;-)

Thoughts? Perhaps I should toss the thing up and set a timer to come stop it, and unhook that from anything that's actually happening in the WebView?

Upvotes: 4

Views: 1995

Answers (2)

cduhn
cduhn

Reputation: 17918

Here's how I handle cases where I need to be sure the page is fully loaded: Add a javascript event handler for the document.onload event that does something like this:

function onDocumentLoad() {
     window.location.href = 'myapp://loaded';
}

Then in your UIWebViewDelegate, you can do this:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[request.URL absoluteString] isEqual:@"myapp://loaded"]) {
        [activityIndicator stopAnimating];
        return NO;
    }
    return YES;
}

Upvotes: 4

Tom Harrington
Tom Harrington

Reputation: 70976

Yeah, as you've found, webViewDidFinishLoad tells you when data has been loaded into the view, but gives no information about what JS might be going on once that's done. Unfortunately UIWebView is pretty limited. The best you can probably do is to poll it with a JS query, via stringByEvaluatingJavaScriptFromString. If you can run some JS to test whether things are finished in the web view, you could do that every half second and dismiss the spinner when you get a positive result.

Upvotes: 0

Related Questions