我最初在这里对这个异常进行了问答:UIWebView show UIActivityIndicator for loading but ignore additional load requests (eg javascript loaded ads) after page initial loading
问题是网页将完成加载,然后开始加载其他元素(即广告、iFrame 等)。解决方案是在页面加载开始时存储当前 URL,如果 url 下次“加载”与加载的最后一个 URL 相同,而不是误报...
(在下面的代码中,网页真正开始加载//show UIActivityIndicator
并真正完成加载主要内容(而不是您正在努力处理的额外内容)//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
}
}