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

时间:2012-11-20 04:31:49

标签: ios uiwebview loading uiactivityindicatorview

这是一篇Q& A帖子。为了通知我在浏览StackOverflow数据库时遇到过这个问题的许多其他用户。这些用户都没有得到一个可靠的答案(他们中的大多数人在几年前提出这个问题,所以我不会碰到这个帖子。)


许多人都在努力解决的问题如下:

当您尝试在UIWebView中加载页面然后页面然后通过iFrame加载另一个页面或者通过javascript加载广告时,您最终会再次调用页面加载函数,如果您使用的是UIActivityIndi​​cator,它将会再次被召唤并惹恼您的用户。

对此的修复是存储以前的MainURL并检查尝试加载的新MainURL,如果匹配则忽略加载功能。 (我在下面的答案中的示例代码)

**在网页已经真正加载后,将在webViewDidStartLoad方法调用{{1}}方法的网页示例如下:www.speakeasy.net/speedtest/

2 个答案:

答案 0 :(得分:5)

//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 (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 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!



//

答案 1 :(得分:1)

我似乎无法让它发挥作用。我的webview被称为&#34; viewWeb&#34;。我的.m文件中有以下代码:

 - (void)webViewDidFinishLoad:(UIWebView *)webView {
    lastURL = [[NSString alloc] initWithFormat:@"%@", viewWeb.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];
    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;

它正在崩溃&#34; lastURL =&#34; webViewDidFinishLoad中的行(lastURL为nil)。我对Xcode很新,所以我对此的了解有限。任何帮助将不胜感激。