I don't know how many forums I have already read, but I really don't know why it doesn't work!
I have an iPhone Application and I would like to show a Epub book to my user.
The user can read it online, otherwise he can download it into the Application Documents directory to read it afterwards.
I build this folder structure same like the online version and saved it into the document folder "frontend".
I ALSO can read this maintaine .HTML file, BUT the linked JS / CSS and HTML files does not work.
So I have some screenshots. I don't know, why the javascript cannot access to the .html docs.
offline version - saved into the filesystem "document folder"
online version - directly from server - it's ok!
I hope you could give me some hints.
I don't know how many forums I have already read, but I really don't know why it doesn't work!
I have an iPhone Application and I would like to show a Epub book to my user.
The user can read it online, otherwise he can download it into the Application Documents directory to read it afterwards.
I build this folder structure same like the online version and saved it into the document folder "frontend".
I ALSO can read this maintaine .HTML file, BUT the linked JS / CSS and HTML files does not work.
So I have some screenshots. I don't know, why the javascript cannot access to the .html docs.
offline version - saved into the filesystem "document folder"
online version - directly from server - it's ok!
I hope you could give me some hints.
Share Improve this question edited Nov 8, 2012 at 13:39 Tony Rad 2,50921 silver badges33 bronze badges asked Nov 8, 2012 at 13:14 iachiach 2474 silver badges21 bronze badges 6- If you are doing a native app, you can use the 'bridge' described here stackoverflow./a/9640486/792677 (intercepting the webView load requests and processing them outside of the webView). – A-Live Commented Nov 8, 2012 at 14:13
- I use [webView loadRequest...] to show my .html from documents. This .html has also ref links with relative paths to css / JS and other html docs. But it does not working... – iach Commented Nov 8, 2012 at 14:14
- Do you mean the js and css are not working at all ? Then you might find useful the link in the second answer of the same question: stackoverflow./questions/5733883/… – A-Live Commented Nov 8, 2012 at 14:15
- I think the problem is, that my html structure isn't in the NSBundle. I generate it dynamically if the user need it. It will store in the document directory, so this method does not work. – iach Commented Nov 8, 2012 at 15:13
- I have got this folder structure in the applications documents folder: Application->Document->mlib->script->xxx.js If my index.html has been loaded into the web view, is it possible to load a .js relatively from he index.html??? – iach Commented Nov 9, 2012 at 10:13
2 Answers
Reset to default 4Simply 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.
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];