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