title: iPhone dev: load a file from resource folder
body: <p>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:</p>
  
  <pre><code>dirA
  |---&gt; index.html 
  |---&gt; a1.html
  |---&gt; a2.html
  |---&gt; my.css
  |---&gt; dirB
        |---&gt; b1.html
        |---&gt; b2.html
  |---&gt; dirC
        |---&gt; c1.html
        |---&gt; c2.html
  </code></pre>
  
  <p>These resources where added to the project as such:</p>
  
  <ul>
  <li>Checked "Copy items into destination groups folder (if needed)".</li>
  <li>Reference type: Default.</li>
  <li>Text encoding: Unicode (utf-8).</li>
  <li>Recursively create groups for any added folders.</li>
  </ul>
  
  <p>The links in my html are relative, meaning they look like this:</p>
  
  <pre><code>&lt;a href="a1.html"&gt;a1&lt;/a&gt;
  &lt;a href="a2.html"&gt;a2&lt;/a&gt;
  &lt;a href="dirB/b2.html"&gt;b2&lt;/a&gt;
  &lt;a href="dirC/c1.html"&gt;c1&lt;/a&gt;
  </code></pre>
  
  <p>In order to display the index.html when the app starts up, I use the following code:</p>
  
  <pre><code>NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
  NSURL *url = [NSURL fileURLWithPath:path];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  [webView loadRequest:request];
  </code></pre>
  
  <p>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 <code>didFailLoadWithError</code> will catch the situation and report that the requested file does not exist.</p>
  
  <p>Also,</p>
  
  <pre><code>[webView loadHtmlString:myHtml];
  </code></pre>
  
  <p>cannot be part of the solution, as I need back and forward buttons to work in my web view.</p>
  
  <p>So the question is: How can I follow a relative link to an html file in a sub directory within my resources?</p>
  
  <p>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!</p>
  
  <p><strong>EDIT:</strong> Yoohoo! I figured it out! What joy! Here is what I did:</p>
  
  <ol>
  <li>Imported my resources anew, choosing "Create folder references for any added folders" instead of "Recursively create groups for any added folders."</li>
  <li>Specified the root directory for resource, like so: <code>NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"dirA"];</code></li>
  </ol>