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
6
  • 7
    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.
    – Rob
    Commented Jul 5, 2012 at 1:34
  • Yes, but it wasn't part of OP's question.
    – user529758
    Commented Jul 5, 2012 at 5:41
  • Agreed. No criticism was intended. Just an observation for user1502286 about a weird little quirk of these otherwise quite intuitive UIWebViewDelegate methods.
    – Rob
    Commented Jul 5, 2012 at 5:45
  • 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". Commented Jul 7, 2012 at 3:48
  • 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.
    – user529758
    Commented Jul 7, 2012 at 4:28
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
  • 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?
    – thefoyer
    Commented Feb 25, 2014 at 22:37
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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.