Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView - Load a local .html file with linked resources

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.

like image 668
iach Avatar asked Feb 19 '23 13:02

iach


1 Answers

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.

like image 118
Yash Vyas Avatar answered Feb 21 '23 04:02

Yash Vyas