Obliviux
Obliviux

Reputation: 2679

How do I check if a WebView is loading a specific page?

I want check if my WebView is loading a specific html page locally (for example index2.html). Is that possible? Thanks!!

EDIT:

I have used this code:

NSString *currentURL
    = webview1.request.URL.absoluteString;

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"indexPRO" ofType:@"html"];

    urlLocation1 = [NSURL fileURLWithPath:filePath];

    if(currentURL = urlLocation1){ //Do something..

but not work.... Is because I use "absolute string" in currentURL and relative string in "urlLocation1"??

Upvotes: 1

Views: 2216

Answers (2)

wkw
wkw

Reputation: 3863

Look at UIWebView delegate methods

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

// examine request to see what url is about to load.
// return YES to allow the load, NO to disallow.
// don't forget to set the delegate:
// myWebView.delegate = self;

    return YES;
}

one of webViewDidFinishLoad or webView:didFailLoadWithError will be called when the request is completed. and as mentioned, checking the "loading" property of your UIWebView object will indicate if something is still be loaded.

if( myWebView.loading ) ...

EDIT: in response to your additional information, (assuming I understand what you're trying to do...) try the following:

// compare scheme property of URL against "file"
if( [[webview1.request.URL scheme] isEqualToString:@"file"] ){
    NSLog(@"load request is local file");
    // ... your code here ...
}

I don't know whether a UIWebView "request" property is valid once the load is finished, haven't tested that. If not, keep a private copy of the most recently loaded URL or request somewhere else.

Upvotes: 3

NSResponder
NSResponder

Reputation: 16861

You can check its "loading" property to see whether it's done or not. You can check the "request" property to get the NSURLReqest to see what it's loading or has loaded.

Upvotes: 1

Related Questions