Close webView page after loading

Asked

Viewed 212 times

1

I’m using webView on iOS to access a web page within a native application using the objective-c language as in the following example:

NSURL *url = [NSURL URLWithString:@"http://www.exemplo.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];

However, I would like the page after fully loaded to close automatically without the user having to interfere. I’ve already searched the NSURL documentation, but I couldn’t find anything to help me with that.

2 answers

1

First, add to your header file .h the class delegate concerning the UIWebView:

@interface WebViewController : UIViewController <UIWebViewDelegate>

And so, in the implementation define the property of your UIWebView:

[_webView setDelegate:self];

And finally, the method delegate which will perform the action you wish, as soon as the page is loaded:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Ação ao carregar página...
}

By "close the page", I didn’t quite understand what exactly this action you want, but I believe from this method you can define.

  • Dexia I see if I understood, the moment the method webViewDidFinishLoad is it called, you mean the page loading is over? For example, if I have an image on the page I am accessing, this method will be called when all the image is loaded on the page?

  • @Jonathangonczoroski, this method will be called when the full page is loaded, if by chance that page is only an image, when you finish loading the image it will be called.

0

In addition to using the webView delegate method : webViewDidFinishLoad

- (void)webViewDidFinishLoad:(UIWebView *)webView {
     // Ação chamada ao terminar de carregar a página...

    // Recomendo adicionalmente uma ação para ser executada automaticamente após alguns segundos depois de carregada a pagina, seja para ocultar a webview ou apresentar uma mensagem ao usuário dizendo que foi com sucesso.
    [self performSelector:@selector(hideWebView) withObject:nil afterDelay:2.0];
}

Also use delegate method to handle page load error situations!

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    //Trate os possíveis erros da carga da url aqui.
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.