I’m having an issue getting local images and css stylesheet to show through on a local HTML page using UIWebView on an iPad app that will be using iOS 5.
The HTML page itself shows through and is laid out well, which is great. But the problem is it has blank squares where images should be coming through, and styling on text or anywhere isn’t showing either.
I’ve come across very similar questions on SO where people have said to load the HTML into the view with the baseURL, which I then did using an answer from their question. But the images and style still did not come through.
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm" inDirectory:@"html_files"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[web loadHTMLString:htmlString baseURL:baseURL];
The UIWebView is put in place using Interface Builder and I have that in the .h file as @property(nonatomic, retain) IBOutlet UIWebView *web;
Inside my html_files folder I have:
- index.htm
- img (folder)
- style (folder)
Using the following code, the log showed "index.htm", img, style which tells me it’s keeping the directories the same, but I still even tried placing and then linking to images in the root of html_files but still nothing.
NSString *htmlPath = [path stringByAppendingPathComponent:@"html_files"];
NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:htmlPath];
NSLog(@"%@", dirContents);
And then within index.htm I’m linking to images like <img src="img/logo.jpg">
Any ideas would be much appreciated, thank you.
EDIT: Added link to project here: http://www.filedropper.com/localhtml (it’s literally just a UIWebView with the HTML files so nothing big and messy)
Okay, here a re the mistakes you made, i said just use only image name in the img tag, you where using the path.
In iOS not mater how many folders you have, where ever you have the resources, it just adds all the resources in a single bundle.
Here is the link, check out i have made the changes only in index.htm