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..
4 Answers
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
-
7Incidentally, in
didFailLoadWithError
, if you ever wanted to do something a like inform the user of some web browsing problem, it's worth noting thaterror.code == NSURLErrorCancelled
is not a fatal error, but rather an indication that theUIWebView
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 indidFailLoadWithError
, you may want to check forerror.code != NSURLErrorCancelled
.– RobCommented Jul 5, 2012 at 1:34 -
Yes, but it wasn't part of OP's question.– user529758Commented 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.– RobCommented 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.– user529758Commented Jul 7, 2012 at 4:28
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
-
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?– thefoyerCommented Feb 25, 2014 at 22:37
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)
- (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;
}