I originally did a Q&A of this anomaly here: UIWebView show UIActivityIndicator for loading but ignore additional load requests (e.g. javascript loaded advertisements) after page initially loads
The problem is that the webpage will finish loading and then it begins loading other elements afterwards (i.e. advertisements, iFrames, etc.)
The solution is to store the current URL when page loading begins and the next time it is "Loading" if the url is the same as the last URL loaded than it's a false positive...
(In the code below the web page truly starts loading on //show UIActivityIndicator
and truly finishes loading the main content (not the extra content you are struggling with) on //hide UIActivityIndicator
)
//Define the NSStrings "lastURL" & "currentURL" in the .h file.
//Define the int "falsepositive" in the .h file. (You could use booleans if you want)
//Define your UIWebView's delegate (either in the xib file or in your code)
- (void)webViewDidFinishLoad:(UIWebView *)webView {
lastURL = [[NSString alloc] initWithFormat:@"%@", webView.request.mainDocumentURL];
if (falsepositive != 1) {
NSLog(@"Loaded");
//hide UIActivityIndicator
} else {
NSLog(@"Extra content junk (i.e. advertisements) that the page loaded with javascript has finished loading");
//This method may be a good way to prevent ads from loading hehe, but we won't do that
}
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
NSURL *requestURL =[request mainDocumentURL];
currentURL = [[NSString alloc] initWithFormat:@"%@", requestURL]; //not sure if "%@" should be used for an NSURL but it worked...
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
if ([currentURL isEqualToString:lastURL]) {
falsepositive = 1;
NSLog(@"The page is loading extra content with javascript or something, ignore this");
} else {
falsepositive = 0;
NSLog(@"Loading");
//show UIActiviyIndicator
}
}