Damian Nils
Damian Nils

Reputation: 81

UIActivityIndicator with UIWebView loadRequest?

So I have a view containing a web view with HTML content. This is my code to display it :

NSURL *url = [NSURL URLWithString:urlWeb];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[vue loadRequest:request];
vue.scalesPageToFit = YES;

This works well. But of course there is a little latency before the HTML content is displayed, and I want to display a UIActivityIndicator at this moment.

I know that I must use :

[myActivityIndicator startAnimating];
[myActivityIndicator stopAnimating];

The only thing I miss is how to know when the HTML content is completely loaded ?

Thanks for your advices !

Upvotes: 4

Views: 3234

Answers (3)

Marco Pace
Marco Pace

Reputation: 3870

There is a Protocol that you can implement to know that, UIWebViewDelegate. If you implement the method webViewDidFinishLoad you can know when the content are completely loaded:

Here is your code:

[activityIndicator startAnimating];

NSURL *url = [NSURL URLWithString:urlWeb];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[vue loadRequest:request];
vue.scalesPageToFit = YES;

And here is the protocol methods

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

Remember to set your class as a delegate of the UIWebView before load the request.

Upvotes: 12

BBog
BBog

Reputation: 3650

You can use the didFinishLoading method of the UIWebView delegate. More info here: UIWebView finished loading Event and, of course, on apple http://developer.apple.com/library/ios/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html

Upvotes: 2

xda1001
xda1001

Reputation: 2459

In UIWebViewDelegate method,

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [myActivityIndicator stopAnimating];
    //myActivityIndicator.hidden = YES;
}

Upvotes: 3

Related Questions