2

I want to add activity indicator to a web view. But i don't know when web view finish loading. I start animating in viewdidload..

0

4 Answers 4

19

You shouldn't start animating in viewDidLoad. Conform to the

UIWebViewDelegate

protocol and make your web view's delegate your view controller, then use the delegate methods:

@interface MyVC: UIViewController <UIWebViewDelegate> {
    UIWebView *webView;
    UIActivityIndicatorView *activityIndicator;
}

@end

@implementation MyVC

- (id)init
{
    self = [super init];
    // ...

    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.frame = CGRectMake(x, y, w, h);
    [self.view addSubview:activityIndicator];

    webView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, w, h)];
    webView.delegate = self;
    // ...
    return self;
}

- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq
{
    [activityIndicator startAnimating];
    return YES;
}

- (void)webViewDidFinishLoading:(UIWebView *)wv
{
    [activityIndicator stopAnimating];
}

- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error
{
    [activityIndicator stopAnimating];
}

@end
Sign up to request clarification or add additional context in comments.

6 Comments

Incidentally, in didFailLoadWithError, if you ever wanted to do something a like inform the user of some web browsing problem, it's worth noting that error.code == NSURLErrorCancelled is not a fatal error, but rather an indication that the UIWebView is going to try to go to another page (either because the user tapped on a link while the load was in progress, or sometimes even because the web site, itself, is redirecting the user). In short, if you ever plan on doing anything more comprehensive in didFailLoadWithError, you may want to check for error.code != NSURLErrorCancelled.
Yes, but it wasn't part of OP's question.
Agreed. No criticism was intended. Just an observation for user1502286 about a weird little quirk of these otherwise quite intuitive UIWebViewDelegate methods.
Thank you. I successfully add activity indicator view to my web view. But there is another problem, I followed your code, but my program didn't load "-(id) init".
That is not to be taken literally :) If you use InterfaceBuilder, some pther methods may be called instead of the explicit init, such as initWithNibName:Bundle: etc.
|
5

Implement the UIWebViewDelegate protocol These are the delegates you need to implement in your code:

- (void)webViewDidStartLoad:(UIWebView *)webView; //a web view starts loading
- (void)webViewDidFinishLoad:(UIWebView *)webView;//web view finishes loading
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; //web view failed to load

1 Comment

Is it normal for these functions to be fired multiple times if the website I am viewing has multiple frames in it? How can these functions fire for only the parent window?
2

You will want to listen for the web view delegate callbacks to correctly show your activity indicator.

Specifically you will want to listen for:

webViewDidStartLoad: (start your activity indicator animation)

webViewDidFinishLoad: (end it)

webView:didFailLoadWithError: (end it)

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html

Comments

0
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.webViewRef.delegate = self;
NSURL *websiteUrl = [NSURL URLWithString:Constants.API_TERMS_AND_CONDITIONS_URL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webViewRef loadRequest:urlRequest];
}

#pragma mark
#pragma  mark -- UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
[self.activityIndicator startAnimating];
return YES;
}
 - (void)webViewDidStartLoad:(UIWebView *)webView{
[self.activityIndicator startAnimating];
 }
  - (void)webViewDidFinishLoad:(UIWebView *)webView{
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
 }
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.