Load resources from relative path using local html in uiwebview
This is how to load/use a local html with relative references.
- Drag the resource into your xcode project (I dragged a folder named www from my finder window), you will get two options "create groups for any added folders" and "create folders references for any added folders".
- Select the "create folder references.." option.
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)}