Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a UIWebview with a local HTML file from a nested folder structure

Specifically, I have a folder structure that looks like the below:

  • about (main folder)
    • css (this sub-folder contains the css files)
    • img (this sub-folder contains the img files)
    • js (this sub-folder contains the js files)
    • page (this subfolder contains the index.html file)

If I click on the index.html file from a normal computer browser, everything works as expected.

However, I am trying to load this index.html into a UIWebView.

So far, what I've done is I dragged the "about" folder to XCode and copied it there as I would any other file. Then I tried the following code:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:baseURL];

The webview loads the index.html, however it doesnt load the images/css/js, as I presume it can't find them within the folder structure.

Any help would be appreciated! Thank you!

like image 773
kurisukun Avatar asked Feb 19 '23 21:02

kurisukun


1 Answers

oops, I actually found the answer here: Load resources from relative path using local html in uiwebview

I was able to use the actual folder structure as is with the following code:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"/about/page"]];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
like image 92
kurisukun Avatar answered Feb 22 '23 09:02

kurisukun