Seb
Seb

Reputation: 37

UIWebView, determine if page has loaded

I would like to know when my WebView has finished loading a page. Im quite new to this and I wonder if you have any suggestions. I need to create a "Loading screen" while the page is loading. This is my basic code:

- (void)viewDidLoad {


NSString *urlAddress = @"http://google.se";

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}

Thanks!

Upvotes: 3

Views: 3702

Answers (3)

Stanislav S.
Stanislav S.

Reputation: 353

I have poor experience with DOM but after some searching I found that the document.readyState is the great option.

From w3schools:

Definition and Usage The readyState property returns the (loading) status of the current document.

This property returns one of four values:

uninitialized - Has not started loading yet loading - Is loading interactive - Has loaded enough and the user can interact with it complete - Fully loaded

So I'm using this to know when UIWebView has loaded the document:

- (void)readyState:(NSString *)str
{ NSLog(@"str:%@",str);

if ([str isEqualToString:@"complete"]||[str isEqualToString:@"interactive"]) {
    NSLog(@"IT HAS BEEN DONE");
    [pageLoadingActivityIndicator stopAnimating];
}

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
 //other code...
 [self readyState:[browserWebView stringByEvaluatingJavaScriptFromString:str]];
}

Upvotes: 0

David Rönnqvist
David Rönnqvist

Reputation: 56635

There are two different callbacks when the view finishes loading, successful load and failed load. They are both part of the the UIWebViewDelegate protocol.

Implement it and set yourself as the delegate for your web view. Then implement

- (void)webViewDidFinishLoad:(UIWebView *)webView

to know when the loading has finished, so that you can remove your loading screen, and implement

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

to know if it failed so that you can show and appropriate message to the user instead of an infinite loading screen.

Upvotes: 8

Tobi Nary
Tobi Nary

Reputation: 4596

Have a look at the UIWebViewDelegate Protocoll. If you set your delegate, you receive messages like – webViewDidFinishLoad:

that's what you want, isn't it?

Upvotes: 3

Related Questions