本文介绍了UIWebView显示UIActivityIndi​​cator用于加载但在页面初始加载后忽略其他加载请求(例如,加载javascript的广告)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a Q&A post. To inform the many other users I saw who struggled with this issue as I browsed through StackOverflow's database. None of these users ever got a solid answer (and most of them were posing the question years ago so I wasn't going to bump the post).

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

The issue many struggled with is as follows:

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

When you try to load a page in a UIWebView and the page then loads another page maybe via an iFrame or it loads an advertisement through javascript you end up getting the page loading function called again and if you are using a UIActivityIndicator it will get called again as well and annoy your users.

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

The fix for this is to store the previous MainURL and to check the new MainURL that is trying to be loaded, if they match than ignore the load function. (Example code in my answer below)

**将调用 webViewDidStartLoad 方法的网页示例网页已经真正加载后的时间是:www.speakeasy.net/speedtest /

**An example of a webpage that will call the webViewDidStartLoad method additional times after the web page has already really loaded would be: 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 (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!



//

这篇关于UIWebView显示UIActivityIndi​​cator用于加载但在页面初始加载后忽略其他加载请求(例如,加载javascript的广告)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:30