1

Here is my scenario:

I have a folder called "HTML" in my bundle. This folder has an HTML file and 5 images. The HTML file references all the images at some point using a simple img tag.

     <img src="tut_navigation.png" />

Now I have a UIWebview which I used to load the HTML file. All of the contents of the HTML file are rendered correctly except for my images. My images are not showing up for some strange reason. Then I did some research and found related posts saying to load URL like so:

NSString *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"tutorial" ofType:@"html" inDirectory:@"HTML"]];
NSString* htmlString = [NSString stringWithContentsOfFile:url encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseURL];

Once I made this change and ran it again, this time only the first image is loading up, the others ones are not. I'm lost as to what the issue is. When I view this HTML file in a web browser, everything is working fine.
Any ideas?

Tulon
  • 4,011
  • 6
  • 36
  • 56
Lavvo
  • 1,024
  • 3
  • 16
  • 35
  • Can you post the HTML file, or at least all of the img tags? – Andrew May 24 '14 at 21:18
  • You can get lot of infos about this on this post : [iPhone Dev: UIWebView baseUrl to resources in Documents folder not App bundle][1] [1]: http://stackoverflow.com/questions/1926117/iphone-dev-uiwebview-baseurl-to-resources-in-documents-folder-not-app-bundle – mad_mask May 24 '14 at 21:22
  • this is an example of how i used the img tags – Lavvo May 24 '14 at 21:27
  • @mad_mask I looked at that link and did it the way it was suggested there and I'm still getting the same result. This is driving me up the wall, only one of the images is loading and the rest are not, so strange. – Lavvo May 24 '14 at 23:16

1 Answers1

1

The baseURL needs to be the a path to the HTML folder inside the app bundle, not (as you have it) a path to the app bundle itself.

Change this:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseURL];

To this:

[self.webView loadHTMLString:htmlString baseURL:url];

You had the right URL for the base URL (url) and then you just threw it away and substituted the URL of the bundle itself, which is wrong.

Even better, forget the base URL and just use a URL instead of a path string for the file itself:

NSString *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"tutorial" ofType:@"html" inDirectory:@"HTML"]];
NSURLRequest* req = [[NSURLRequest alloc] initWithURL:url];
[self.webview loadRequest: req];
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I believe it is, I've re-edited my original post to show the full code. – Lavvo May 24 '14 at 23:14
  • No it's not. Look at your code and how you calculate the `baseURL`. You are up one level from the HTML folder - at the main bundle itself. That's the problem. – matt May 24 '14 at 23:20
  • Basically I think you've just chosen the variable names so poorly that you've confused the heck out of yourself. I've edited my answer to hold your hand a bit more... :) – matt May 24 '14 at 23:22
  • wow, thanks @matt. Your suggestion worked. I used your second suggestion. – Lavvo May 24 '14 at 23:42
  • Yeah, the second way is much cleaner. The only downside is that this URL goes into the web view's history; in some cases you might not want that. – matt May 24 '14 at 23:49