1

我的应用程序中有一个 webview。它加载了大部分页面,并且在加载完成时调用了函数 webViewDidFinishLoad。但是尽管页面似乎已加载,但某些页面加载并未完成。在这些情况下,既不会 - (void)webViewDidFinishLoad:(UIWebView *)webView也不会(void)webView:(UIWebView *)webView didFailLoadWithError被调用。例如:转到站点http://www.mynevadacounty.com/,当您单击任何项​​目(例如移动设备)时,页面加载不会在 UIWebView 中完成。

这可能是什么原因?

编辑:我试图加载的这些网站中的大多数都是共享点网站

4

1 回答 1

3

我最初在这里对这个异常进行了问答:UIWebView show UIActivityIndi​​cator 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
    }
}
于 2013-01-12T03:52:21.503 回答