Uiwebview - Load A Local .html File With Linked Resources
Solution 1:
Simply write the code
NSString*path = [[NSBundle mainBundle] pathForResource:@"offlineEpub" ofType:@"html"];
NSString*content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
UIWebView*webView = [[UIWebView alloc] init];
[webView loadHTMLString:content baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self.view addSubview:webView];
The main thing is provide base url as your mainBundle or documents directory.
Solution 2:
After much fiddling around and reading a lot of answers, here is my simple consolidated solution:
1) Make sure all your bundle resources are unique---directories get flattened
It is super frustrating, but if you had directories "About/index.html" and "Home/index.html", one of the last "index.html" to get copied into the bundle will overwrite the previous.
2) JavaScript files will be in the wrong Build Phase
In Xcode click on your project, then click on your app target, then click on Build Phases. Notice the JavaScript files will wrongly be in Compiled Sources: move JS files into Copy Bundle Resources.
3) Use UIWebView's loadRequest:
Don't load from a string. Use an NSURL instead.
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *url = [mainBundle URLForResource:@"some_page" withExtension:@"html"];
NSURLRequest *urlReq = [NSURLRequest requestWithURL:url];
// Assuming you create an IBOutlet defined like:// @property (strong, nonatomic) IBOutlet UIWebView *wv;
[self.wv loadRequest:urlReq];
Post a Comment for "Uiwebview - Load A Local .html File With Linked Resources"