这是一个Q&A后。 要通知其他许多用户,我看到谁这个问题挣扎,因为我通过StackOverflow上的数据库浏览。 这些用户都没有拿到过一个坚实的答案(其中大多数是问题年前冒充,所以我不打算撞击后)。
许多挣扎的问题如下:
当您尝试在一个UIWebView加载页面,然后在页面可能加载其他页面通过iframe或再次加载通过JavaScript你最终得到所谓的页面加载功能的广告,如果你使用的是UIActivityIndicator它会再次调用也惹恼你的用户。
造成这种情况的解决方法是存储先前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 (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!
//
我似乎无法得到这个工作。 我的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 (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;
据拍击webViewDidFinishLoad内的“最后URL =”行(最后URL是无)。 我是很新的Xcode的,所以我在这方面的知识是有限的。 任何帮助将非常感激。
文章来源: UIWebView show UIActivityIndicator for loading but ignore additional load requests (e.g. javascript loaded advertisements) after page initially loads