Ethan Humphries Ethan Humphries - 4 months ago 54
iOS Question

webViewDidFinishLoad not working?

In my app, I am trying to make a splash image appear as my UIWebView loads so it is not just a blank screen. However my webViewDidFinishLoad method will not work. This means that the splash image appears but does not disappear from the screen once the UIWebView has loaded.

My code for the method is:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"content loading finished");
[loadingImageView removeFromSuperview];
}


Any help on why the method will not work would be appreciated greatly.

My .h:

@interface ViewController : UIViewController

-(IBAction)makePhoneCall:(id)sender;

@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property(nonatomic, strong) UIImageView *loadingImageView;

@end


My ViewDidLoad and webViewDidFinishLoading:

- (void)viewDidLoad {
UIWebView *mWebView = [[UIWebView alloc] init];
mWebView.delegate = self;
mWebView.scalesPageToFit = YES;

[super viewDidLoad];
}


//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sleafordpizza.com/food"]]];


//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:@"LittleItalyLogo.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"LittleItalyLogo.png"],
nil];
[self.view addSubview:loadingImageView];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"content loading finished");
// Remove loading image from view
[loadingImageView removeFromSuperview];
}

Answer

Hi probably you do not set proper delegate.

This is small code tip for you.

-(void)viewDidLoad {

    mWebView = [[UIWebView alloc] init];
    mWebView.delegate = self;
    mWebView.scalesPageToFit = YES;
}

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

  [loadingImageView removeFromSuperview];  
   NSLog(@"finish");   
}

In you're .h file add.

@interface MyView: UIViewController <UIWebViewDelegate> {
           UIWebView *webView;
}

Code fixes.

For .h file

@interface ViewController : UIViewController<UIWebViewDelegate>


-(IBAction)makePhoneCall:(id)sender;

@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property(nonatomic, strong) UIImageView *loadingImageView;


@end

For .m file

- (void)viewDidLoad
{


[super viewDidLoad];

webView.delegate = self;

//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sleafordpizza.com/food"]]];


//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:@"LittleItalyLogo.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
                                    [UIImage imageNamed:@"LittleItalyLogo.png"],
                                    nil];
[self.view addSubview:loadingImageView];

}