0

Within UIWebView:shouldStartLoadWithRequest:, I'd like to know if the page being loaded is a local file or a remote file. Is there an easy way of finding this out?

I suppose every time a file is loaded I could search the filesystem looking for the file, but is there another way?

dgund
  • 3,459
  • 4
  • 39
  • 64
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378

2 Answers2

1

You should be able to tell the kind of request it is by the kind of URL.

It should be as simple as file:… vs http:… or https:….

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • What schemes are you expecting to support? [Apple](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i) claims support for ftp, http, https, and file. – Jeffery Thomas Oct 02 '12 at 21:22
  • Checking for just http and https wouldn't be complete as there's many other schemes. Checking for file: would be ok unless a http file was first loaded which could itself contain a file: link to a local file on its file system, if so then checking for file: would therefore not work in this situation as it would falsely report that a remote file was local. - Can an html file contain a "file://..." link? – Gruntcakes Oct 02 '12 at 21:22
  • A UIWebView should be capable of supporting any scheme, I've seen it load others than those few listed, I need to support all and every scheme there is. – Gruntcakes Oct 02 '12 at 21:23
  • As to the second part of your comment: subrequests are an entirely different problem all together. – Jeffery Thomas Oct 02 '12 at 21:26
  • I think what you want is impossible. I could image a URL that encodes an instruction for both local and remote data. – Jeffery Thomas Oct 02 '12 at 21:34
0

Yes it is possible. You should use a regular expression to test against the hostname. Local pages won't match the regular expression.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    static NSString *regexp = @"^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9])[.])+([A-Za-z]|[A-Za-z][A-Za-z0-9-]*[A-Za-z0-9])$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexp];

    if ([predicate evaluateWithObject:request.URL.host]) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return NO; 
    } else {
        return YES; 
    }
}
KPM
  • 10,558
  • 3
  • 45
  • 66
  • Note: the regular expression is derived from http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address – KPM Oct 02 '12 at 22:22