This is how to load/use a local html with relative references.
Use the below given code. It should work like a charm.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
Now all your relative links(like img/.gif, js/.js) in the html should get resolved.
Swift 3
if let path = Bundle.main.path(forResource: "dados", ofType: "html", inDirectory: "root") {
webView.load( URLRequest(url: URL(fileURLWithPath: path)) )
}
In Swift:
func pathForResource( name: String?,
ofType ext: String?,
inDirectory subpath: String?) -> String? {
// **name:** Name of Hmtl
// **ofType ext:** extension for type of file. In this case "html"
// **inDirectory subpath:** the folder where are the file.
// In this case the file is in root folder
let path = NSBundle.mainBundle().pathForResource( "dados",
ofType: "html",
inDirectory: "root")
var requestURL = NSURL(string:path!)
var request = NSURLRequest(URL:requestURL)
webView.loadRequest(request)
}
I crammed everything into one line (bad I know) and had no troubles with it:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test"
ofType:@"html"]
isDirectory:NO]]];
I simply do this:
UIWebView *webView = [[[UIWebView alloc] init] autorelease];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
Where "index.html" relatively references images, CSS, javascript, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With