I have a webview in my iPhone application, and I also have some html files inside my Resources folder. When my app loads, I load in a page from my resources into my webview.
OK, here 3.5 years later :) ... tried Michael Gaylord's suggestion above exactly, but it didn't work in sims for 5.0, 5.1, 6.0, 6.1 - nor in real devices running same. However this SO clarification on where resources are bundled ( Where is build output going? ) helped make it work for me like this:
Make sure the project recognizes that it should be in the bundle by:
So all I had to do was copy - paste Michael's code above verbatum and change this one line:
inDirectory:@"html"]
to:
inDirectory:nil]
and Shaazam ! it worked :)
When you load those resources, you need to set the base URL. You can do this using the method:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
...where baseURL would be the file URL of your resources folder.
Apparently there's a bit of a gotcha according to the iPhone SDK Release Notes for iPhone OS 3.0:
Issue: UIWebView can't load local resources in apps built against 3.0.
When using
[UIWebView loadHTMLString:baseURL:]
, the HTML string should not refer to local resources with thefile://
scheme. Instead, pass inNULL
or afile://
URL forbaseURL:
, or include the resources directly in the HTML with<style>
and<script>
tags.
Fair enough. But passing in a standard-issue file://
URL for baseURL
doesn't quite work. However ... it turns out that, if you change /
to //
and spaces to %20
, you will be set! (Using %20
makes a lot of sense, but the double-slash part caught me by surprise.)
Let's say you already have NSString *markup
set up. Here's all you do. (I've wrapped the code here for readability. You may wish to refactor/adjust to taste.)
NSString *resourcePath = [[[[NSBundle mainBundle] resourcePath]
stringByReplacingOccurrencesOfString:@"/" withString:@"//"]
stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
[webView loadHTMLString:markup baseURL:[NSURL URLWithString:
[NSString stringWithFormat:@"file:/%@//", resourcePath]]];
So long as your CSS, JavaScript and images are referred to by filename alone, this should do the trick in iPhone OS 3.0 where loadHTMLString:baseURL:
is concerned!
This code would return a string with the URL of a file named "OtherPage.html" in your bundle's resources directory.
[[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"OtherPage" ofType:@"html"]] absoluteString]
This worked for me. My HTML files were in sub-folders (not groups) in my project. My html page is called page01 and the sub-folder is html:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *pathToHtml = @"html";
NSURL *baseURL = [[NSURL alloc] initFileURLWithPath:pathToHtml isDirectory:YES];
NSString *html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"page01" ofType:@"html"
inDirectory:@"html"]
encoding:NSUTF8StringEncoding error:nil];
[webview loadHTMLString:html baseURL:baseURL];
For me the problem was that js files were not included in the bundle. Be sure to check that.