views:

179

answers:

3

Hi guys, I have this question that is unsolved for me.... I need to check if the current URL of my webview is a local file in the Documents directory of my app....how can I do this??

Thanks!

A: 

Check for the file:// protocol in the URL.

Pierre-Antoine LaFayette
+2  A: 

Check the URL's scheme. Assuming you have an NSURL* instance called myURL:

if ([[myURL scheme] isEqualToString:@"file"]) {
    NSLog(@"URL %@ is local", myURL);
}

Read Apple's NSURL specification to learn more about accessing different parts of an URL.

Assuming you have a UIWebView* called myWebView, you might call something like:

[[myWebView request] URL]

to get the NSURL* of what the web view currently has loaded. Then you can call that object's scheme as described above.

Alex Reynolds
Thanks! What is the URL scheme for my file "index.html" contained in the Documents folder of my app?? :)
Obliviux
Should just be `@"file"`, not `@"file://"`.(Also, `-isFileURL` is a bit more concise.)
Wevah
You're right about the scheme. Changed.
Alex Reynolds
+5  A: 

You can check:

[request.URL isFileURL];

More information here.

fbrereto