UIWebView show UIActivityIndicator for loading but ignore additional load requests (e.g. javascript loaded advertisements) after page initially loads

杀马特。学长 韩版系。学妹 提交于 2019-12-21 03:12:05

问题


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:

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.

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)

**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/


回答1:


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



//



回答2:


I can't seem to get this to work. My webview is called "viewWeb". I have the following code in my .m file:

 - (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
    }
}

within my .h, I have:

NSString *lastURL;
NSString *currentURL;
BOOL falsepositive;

It is crashing on the "lastURL =" line within the webViewDidFinishLoad (lastURL is nil). I am very new to Xcode, so my knowledge on this is limited. Any help would be much appreciated.



来源:https://stackoverflow.com/questions/13466621/uiwebview-show-uiactivityindicator-for-loading-but-ignore-additional-load-reques

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!