Download UIWebview with local HTML file from subfolder structure

In particular, I have a folder structure that looks like this:

  • about (main folder)
    • css (this subfolder contains css files)
    • img (this subfolder contains img files)
    • js (this subfolder contains js files)
    • (this subfolder contains the index.html file)

If I click on the index.html file from a normal browser, everything will work as expected.

However, I am trying to load this index.html into a UIWebView .

So far, what I have done, I dragged the "about" folder in Xcode and copied it there, like any other file. Then I tried the following code:

 NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile]; NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]; [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:baseURL]; 

The webview loads index.html, however it does not load the images / css / js as I believe that it cannot find them in the folder structure.

Any help would be appreciated! Thanks!

+4
source share
2 answers

oops, I really found the answer here: Loading resources from a relative path using local html in uiwebview

I managed to use the actual folder structure, as is, with the following code:

 NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"/about/page"]]; [webView loadRequest:[NSURLRequest requestWithURL:url]]; 
+8
source

Instead of using images/css/js use only sample.js .

0
source

All Articles