4

Specifically, I have a folder structure that looks like the below:

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

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

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

So far, what I've done is I dragged the "about" folder to XCode and copied it there as I would 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 the index.html, however it doesnt load the images/css/js, as I presume it can't find them within the folder structure.

Any help would be appreciated! Thank you!

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
kurisukun
  • 3,149
  • 5
  • 37
  • 69
  • I don't think folder structures are carried over to the iOS app binary. You'll need unique filenames. – Ryan Poolos Jul 10 '12 at 12:29
  • all files that you submit your application, including those contained in sub folders, are placed in the root directory. – WhiteTiger Jul 10 '12 at 12:35

2 Answers2

8

oops, I actually found the answer here: Load resources from relative path using local html in uiwebview

I was able 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]];
Community
  • 1
  • 1
kurisukun
  • 3,149
  • 5
  • 37
  • 69
0

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

Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39