add indicator for UIwebview loading in iOS

To achieve this,

we need to do several steps:

1. make self as the webview delegate : UIWebViewDelegate.

2. add a UIActivityIndicatorView into the view

3. implement methods like start load in the UIWebViewDelegate;

Below are the details.

 1. make self as the webview delegate : UIWebViewDelegate.

in the .h file, add <UIWebViewDelegate> after the UIViewController.  for example :

@interface VideoSearchViewController : UIViewController<UIWebViewDelegate>

in the .m file. add yourWebView.delegate = self; This step is important, otherwise the delegate methods won’t be called.

2. add a UIActivityIndicatorView into the view

in the storyboard, drag a “UIActivityIndicator” to the center of your webview and set the property Hides when stopped.

create outlet for it: hold control and drag it to the .h file, then name it. This will create something like:

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicatior;

add synthesiize for the “activityIndicatior” in the .m file.

3. implement methods for UIWebViewDelegate;

implement below methods:

 

#pragma spinner
- (void)webViewDidStartLoad:(UIWebView *)searchWebView {

    [ activityIndicatior startAnimating];
}

- (void)webView:(UIWebView *)searchWebView didFailLoadWithError:(NSError *)error {

    [activityIndicatior stopAnimating];

}

- (void)webViewDidFinishLoad:(UIWebView *)searchWebView {

    [activityIndicatior stopAnimating];
    activityIndicatior.hidden = YES;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s