Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping network activity indicator

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];
like image 213
isarathg Avatar asked Jan 23 '23 07:01

isarathg


2 Answers

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?

like image 66
Mark Adams Avatar answered Jan 26 '23 05:01

Mark Adams


You just need to set webView.delegate to point to some object, and have that object implement webViewDidFinishLoad:.

like image 36
Tom Avatar answered Jan 26 '23 05:01

Tom