user1502286
user1502286

Reputation: 27

Add activity indicator to web view

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..

Upvotes: 2

Views: 19283

Answers (4)

Mannam Brahmaiah
Mannam Brahmaiah

Reputation: 2283

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

Upvotes: 0

user529758
user529758

Reputation:

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

Upvotes: 19

rooster117
rooster117

Reputation: 5552

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

Upvotes: 2

Midhun MP
Midhun MP

Reputation: 107121

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

Upvotes: 5

Related Questions