views:

3515

answers:

3

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">a2</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"];
A: 

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.

Cliff
Thanks for the quick reply! The folders are actual folders on the file system, drag/dropped exactly as you suggest into XCode. In the build folder (and on the deployed folder in the simulator), the resources are all flattened out. No hierarchy there. I confirmed this, by changing one of my links to point to a root resource of the file name that should be in a sub directory; the request went through. Any other suggestions?
thomax
+4  A: 

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

zaph
Yes, thanks. As you can see of my edit (of the original question), I discovered this by myself some hours age. Seems there is no "i finally figured it out myself"-bit. Oh well. I'll just give you the credit :-)
thomax
I would like to obtain the same result but I have already added the folders. Is there anyway to teach to xcode to organize that in folders afterwards?
amok
A: 

would it be possible for someone to paste the AppDelegate code files and the ViewController code files because I can't tell where/which file to put the code I've found. I'm hoping to load a local web page in the UIWebView

Thanks, B

BoNak