4

I've got some HTML and some images in my iPhone app, arranged something like:

html/
    foo.html
images/
    bar.png

I can get bar.png to appear in my UIWebView a couple of different ways -- either loading foo.html from an NSUrl, and walking back up the directory tree from the html directory:

<img src="../images/bar.png"/>

or by loading foo.html into a string, using loadHtmlString, and using [[NSBundle mainBundle] bundleURL] as the baseURL:

<img src="images/bar.png"/>

Both of these are kind of clumsy, though -- in the first case, if I move HTML files around I have to rejigger all the relative paths, and in the second case, I have to ignore the actual path structure of the HTML files.

What I'd like to make work is this --

<img src="/images/bar.png"/>

-- treating the bundleURL as the root of the "site". Is there any way to make this work, or am I doomed to have that translated into file:///images/bar.png and have the file not found?

David Moles
  • 48,006
  • 27
  • 136
  • 235
  • I'm looking to do the exact same thing. Did you ever find a solution to this problem? – Heiberg Dec 13 '11 at 09:23
  • I'll ask, since it's a couple of years later: did you ever find a good way to use root-relative URLs to content in the iOS filesystem? Outside of running a local web server, I haven't found a sane way to do this -- I would even be happy to hack things by rewriting UIWebView's requests, provided there were a place to do so. – Dave Peck Mar 05 '14 at 21:42
  • Sadly, no, but I haven't been doing IOS development for a couple of years now. – David Moles Mar 05 '14 at 22:12
  • @DavePeck I think i see a way to implement this without going for hacks like custom web server :). One need to implement custom schema or a redirection using NSURLProtocol, once i have nice solution i'll post an answer. – Piotr Czapla Oct 05 '14 at 11:26

2 Answers2

1

Only way I can see for you to do this would be to embed a web server in your app. Matt Gallagher has a blog post on this you could start from. Alternatively, CocoaHTTPServer and Mongoose could be dropped into your project.

Dan Bennett
  • 1,450
  • 1
  • 15
  • 17
0

If I'm not mistaken, you have some files in your project bundle that you want to load in your web view. You can do it simply with these few lines of code:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"bar" ofType:@"png"];
NSURL    *imageURL  = [NSURL fileURLWithPath:imagePath];

I'm assuming that you have a text/html file containing the pattern for your web view. You'll need to add the image as an object there (src="%@"...) and then add the imageURL to the pattern:

NSString *path = [[NSString alloc]initWithString:[[NSBundle mainBundle]pathForResource:@"htmlPattern" ofType:@"html"]];
NSError *error;
NSString *pattern = [[NSString alloc]initWithContentsOfFile:path 
                                                   encoding:NSUTF8StringEncoding
                                                      error:&error];


htmlPage = [[NSString  alloc]initWithFormat:pattern,
            imageURL; 
webView = [[UIWebView alloc] initWithFrame:WEBVIEW_FRAME];
[webView loadHTMLString:htmlPage baseURL:[NSURL URLWithString:path]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pattern]]];
Neeku
  • 3,646
  • 8
  • 33
  • 43
  • Not on this project any more but that looks like the sort of trick that would work. Thanks! – David Moles Jun 12 '12 at 19:47
  • I don't think this addresses the original question, which is asking for a way to use root-relative URLs. If you're coming from the filesystem, root-relative is by definition going to be the root of the filesystem... what you'd like is to somehow rewrite URLs so that the root is where *you'd* like it to be... – Dave Peck Mar 05 '14 at 21:44