UIWebView没有完成加载?

时间:2013-01-12 00:27:18

标签: ios uiwebview

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

可能的原因是什么?

编辑:我尝试加载的大多数网站都是sharepoint网站

1 个答案:

答案 0 :(得分:3)

我最初在这里做过这个异常的Q& A:UIWebView show UIActivityIndicator for loading but ignore additional load requests (e.g. javascript loaded advertisements) after page initially loads

问题是网页将完成加载,然后开始加载其他元素(即广告,iFrame等) 解决方案是在页面加载开始时存储当前URL,如果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
    }
}