isarathg
isarathg

Reputation: 878

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

Upvotes: 4

Views: 4380

Answers (4)

hanumanDev
hanumanDev

Reputation: 6614

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

Upvotes: 0

Hardik Thakkar
Hardik Thakkar

Reputation: 15951

To show network Indicator while UIWebView load request use below code..

#pragma mark - UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}
//For Showing NetWork Indicator in Webview
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

Note: UIViewController<UIWebViewDelegate> and [webView setDelegate:self];

Upvotes: 0

Mark Adams
Mark Adams

Reputation: 30846

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?

Upvotes: 6

Tom
Tom

Reputation: 3861

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

Upvotes: 3

Related Questions