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!

2
  • I don't think folder structures are carried over to the iOS app binary. You'll need unique filenames. Commented Jul 10, 2012 at 12:29
  • all files that you submit your application, including those contained in sub folders, are placed in the root directory. Commented Jul 10, 2012 at 12:35

2 Answers 2

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]];
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.