I used the below code to load a webpage. Everything works fine, but I want to stop the network activity indicator after completing loading the webpage. How can we know that the webpage is loaded completely.
Anyone please help.
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
The simplest way to do this is to add this line after you instantiate your UIWebView.
[webView setDelegate:self];
Now you will call webViewDidFinishLoad:
and the entire method should look like this.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
I'll explain this further since you will absolutely need to understand delegation in the future.
UIWebView is merely an object that displays web data. It doesn't really like handling all this other stuff, because it really just loves web data. Now in your case you wanted to find out when UIWebView was done doing its favorite little task. To do this, your UIWebView gives a shoutout to it's delegate like "Yo holmes, I'm done loading this data now. So go do whatever it is you do now." and your UIWebView continues on its merry way.
So what we did is we told the UIWebView that its delegate, in this case, was our current class by setting the delgate property on webView to self.
From there you can call any of the methods available to the UIWebView delegate (its all in the documentation) and have them perform tasks secondary to the main purpose of the UIWebView.
Make sense?
You just need to set webView.delegate
to point to some object, and have that object implement webViewDidFinishLoad:
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With