Question

I am trying to load a UIWebview and am trying to implement the SVProgress HUD when the webview is loading and then dismissing it when its loaded.I have almost completed it however when it loads the progress hud shows for a second and then dosent show and then shows for a second and so fourth, like its blinking I'm kinda stuck, check out my code.

In my .h:

#import <UIKit/UIKit.h>
#import "SVProgressHUD.h"


@interface ViewController2 : UIViewController <UIWebViewDelegate> {

//Instantiated the timer varibale inside the interface.
NSTimer *timer1;
IBOutlet UIWebView *webView;


}

//Instantiate the webview ans NSString link.
@property(nonatomic,retain) UIWebView *webView;
@property (strong, nonatomic) NSString *link;

and in my .m

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

@synthesize webView;

- (void)viewDidLoad
{
[super viewDidLoad];

//Getting the link from the NSString called "link" and putting in the request.


NSString *link = [NSString stringWithString:self.link];

    NSURL *URL = [NSURL URLWithString:link];

    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    [webView loadRequest:request];


    //Set the time for the loagind modal view.

    timer1 = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self    selector:@selector(loading) userInfo:nil repeats:YES];




    //Webview Properties
    //Webview's name is web, which was instantianted in the propeties section.

    webView.ScalesPageToFit = true;
    webView.backgroundColor = [UIColor pomegranateColor];


    }

 //When the UIWebview is loading present "Fetching Homework" alert, thanks to SVProgressHUD

-(void)loading {
if(!webView.loading)
    [SVProgressHUD dismiss];
else
    [SVProgressHUD showWithStatus:@"Loading Homework"];
}

I think that it could just be the timer, but I'm not sure, anyhelp would be greatly appreciated.

Thanks in advance.

Was it helpful?

Solution

You should use the web view delegate to know when the page ends loading instead of using a timer to check it each 0.5 seconds. Try it this way:

Begin the SVProgressHUD when inits the page loading

     [SVProgressHUD showWithStatus:@"Loading Homework"];  

Then remove it when the page finish loading.

  - (void)webViewDidFinishLoad:(UIWebView *)webView {
        [SVProgressHUD dismiss];
    }

Don't forget that you also have to set your webview delegate as this:

     webView.delegate = self;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top