0

how can i open local file (html,jpg...etc ) and website url with same uiwebview

this code worked for website url only

NSURL *targetURL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];

[viewWeb loadRequest:request];}

or can i use check if the path input is URL or Local File if yes ? How ?

OsamahM
  • 337
  • 1
  • 2
  • 16
  • Check out UIWebView function - (void)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL; also check out https://stackoverflow.com/a/747644/513286 – cekisakurek Jul 24 '17 at 15:49

1 Answers1

3
  • For an URL with a scheme (e.g.http://) use

    NSURL *targetURL = [NSURL URLWithString:@"http://myserver/path/to/file.html"];
    
  • For a file URL use

    NSURL *targetURL = [NSURL fileURLWithPath:@"/Users/myUser/path/to/file.html"];
    

    or add the scheme

    NSURL *targetURL = [NSURL URLWithString:@"file:///Users/myUser/path/to/file.html"];
    

A file URL starts always with a slash

if ([urlString hasPrefix:@"/"]) {
    // is file URL
} else {
    // is URL including a scheme
}
vadian
  • 274,689
  • 30
  • 353
  • 361