UIWebView显示UIActivityIndi​​cator进行加载,但在页面初始加载后忽略其他加载请求(例如JavaScript加载的广告)

这是一个问答post。 为了告诉其他许多用户,我在StackOverflow的数据库中浏览过这个问题,看到了这个问题。 这些用户都没有得到一个确切的答案(其中大部分是在几年前提出的问题,所以我不会碰到这个职位)。


许多困难的问题如下:

当你尝试加载一个页面在UIWebView和页面然后加载另一个页面可能通过一个iFrame或它加载一个广告通过javascript你最终得到的页面加载函数再次调用,如果你使用的UIActivityIndi​​cator它会再次被调用以及惹恼你的用户。

解决方法是存储以前的MainURL并检查正在尝试加载的新MainURL,如果它们匹配,则忽略加载函数。 (在我的答案下面的示例代码)

**在网页已经真正加载完成后,再次调用webViewDidStartLoad方法的网页示例如下:www.speakeasy.net/speedtest/

 //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 `<UIWebViewDelegate>` in .h and `webView.delegate = self;` in .m viewDidLoad) - (void)webViewDidFinishLoad:(UIWebView *)webView { lastURL = [NSString stringWithFormat:@"%@", webView.request.mainDocumentURL]; if (falsepositive != 1) { NSLog(@"Loaded"); //hide UIActivityIndicator } else { NSLog(@"Extra content junk (ie 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 stringWithFormat:@"%@", requestURL]; //not sure if "%@" should be used for an NSURL but it worked..., could cast `(NSString *)` if we *really* wanted to... 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 } } //make sure you read the //comments// at the top of this code snippet so that you properly define your .h variables O:) Thanks! // 

我似乎无法得到这个工作。 我的webview被称为“viewWeb”。 我的.m文件中有以下代码:

 - (void)webViewDidFinishLoad:(UIWebView *)webView { lastURL = [[NSString alloc] initWithFormat:@"%@", viewWeb.request.mainDocumentURL]; if (falsepositive != 1) { NSLog(@"Loaded"); //hide UIActivityIndicator } else { NSLog(@"Extra content junk (ie 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]; 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 } } 

在我的.h,我有:

 NSString *lastURL; NSString *currentURL; BOOL falsepositive; 

它在webViewDidFinishLoad(lastURL为零)中的“lastURL =”行崩溃。 我对Xcode很新,所以我的知识是有限的。 任何帮助将非常感激。