Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone dev: load a file from resource folder

I'm writing an iPhone app with a UIWebView which should display various html files I have in the app resource folder. In xcode my project overview, these html files are displayed like this:

dirA
|---> index.html 
|---> a1.html
|---> a2.html
|---> my.css
|---> dirB
      |---> b1.html
      |---> b2.html
|---> dirC
      |---> c1.html
      |---> c2.html

These resources where added to the project as such:

  • Checked "Copy items into destination groups folder (if needed)".
  • Reference type: Default.
  • Text encoding: Unicode (utf-8).
  • Recursively create groups for any added folders.

The links in my html are relative, meaning they look like this:

<a href="a1.html">a1</a>
<a href="a2.html">a2</a>
<a href="dirB/b2.html">b2</a>
<a href="dirC/c1.html">c1</a>

In order to display the index.html when the app starts up, I use the following code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

This works fine. Following links from the index file also works fine, as long as the html files requested are directly under dirA. If the link followed points to a file in a sub-directory, then didFailLoadWithError will catch the situation and report that the requested file does not exist.

Also,

[webView loadHtmlString:myHtml];

cannot be part of the solution, as I need back and forward buttons to work in my web view.

So the question is: How can I follow a relative link to an html file in a sub directory within my resources?

I've been all over stackoverflow and the rest of the tubes for the past few days trying to figure this one out, but nowhere have I come across the solution to this exact problem. Any insight at all would be very, very much appreciated!

EDIT: Yoohoo! I figured it out! What joy! Here is what I did:

  1. Imported my resources anew, choosing "Create folder references for any added folders" instead of "Recursively create groups for any added folders."
  2. Specified the root directory for resource, like so: NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"dirA"];
like image 449
thomax Avatar asked Mar 25 '10 17:03

thomax


2 Answers

To create folders in the app bundle drag the folder to Xcode and select the radio button: "Create Folder References for any added folders".

like image 199
zaph Avatar answered Oct 06 '22 10:10

zaph


The subdirectories you use in XCode are groups not actual folders. all of the resources are likely being flattened out to the output folder. If you create actual folders outside of XCode that might work. Try creating the folder and file heirarchy and drag/dropping into XCode. Also check your build folder using Finder to see exactly how XCode is deploying your files.

like image 44
Cliff Avatar answered Oct 06 '22 10:10

Cliff