views:

259

answers:

3

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];

+3  A: 

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

Tom
Can you please explain it. I am new to objective c programming.
iSharreth
+3  A: 

The simplest way to do this is to add this line to after you instantiate your UIWebView.

[webView setDelgate: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?

CrystalSkull
Thank you very much for your explanation!
iSharreth
+1 for "Yo holmes."
Jaanus
I like to imagine that all objects talk like the fresh prince of bel-air.
CrystalSkull
A: 

here's what I used to stop the activity indicator:

  • (void)viewDidLoad {
    [super viewDidLoad];

NSString *urlAddress = [NSString stringWithFormat:@"http://www.wikipedia.com/wiki/%@", place.name];
NSURL *url= [NSURL URLWithString: [urlAddress stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

[webView loadRequest:requestObj];

[[self webView] setDelegate:self];

}

  • (void)webViewDidFinishLoad:(UIWebView *)webView { [activityIndicator stopAnimating]; }

  • (void) webViewDidStartLoad:(UIWebView *)webView { [activityIndicator startAnimating]; }

hanumanDev