2013-01-12 72 views
1

我在我的application.It中加載了大部分頁面,並在加載完成時調用webViewDidFinishLoad函數。但某些頁面加載並未完成,儘管頁面似乎已加載。在這些情況下 - (void)webViewDidFinishLoad:(UIWebView *)webView(void)webView:(UIWebView *)webView didFailLoadWithError都不會被調用。例如:轉到網站http://www.mynevadacounty.com/,當您點擊任何項目(如移動設備)時,UIWebView中的頁面加載沒有完成。UIWebView未完成加載?

這可能是什麼原因造成的?

編輯:大部分這些網站,我試圖加載的是SharePoint網站

回答

3

我本來問答&一個這種異常現象在這裏:UIWebView show UIActivityIndicator for loading but ignore additional load requests (e.g. javascript loaded advertisements) after page initially loads

的問題是,該網頁將完成裝載和那麼它開始後加載其他元素(即廣告,iFrames等) 解決的辦法是在頁面加載開始時存儲當前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 
    } 
}