How to load local html resources in directories (js, images, css) in UIWebview? (IOS)

In particular, how do you get all the resources to download from your respective directories (images /, css /, js /) without changing the contents of the original HTML (deleting directories)?

I use this code to load index.html in a UIWebview:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"]; NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *baseURL = [NSURL fileURLWithPath:path]; [webView loadHTMLString:html baseURL:baseURL]; 

Then my html looks like this:

 ... <link rel="stylesheet" type="text/css" href="css/main.css" /> <script type="text/javascript" src="js/classes.js"></script> <script type="text/javascript" src="js/animate.js"></script> etc... 

If I remove js /, css / and images /, everything works because iOS believes that everything is at the root of my package. But this means that I cannot use the same code for projects without dumping all of it into one directory (llama). The engineer in me cannot handle this disorganization.

Thanks in advance.

+3
source share
2 answers

Convenient: when adding folder (s) to Xcode, make sure that you add them as links to folders.

They will then be added as folders to the .app package:

+3
source

You have to do it. It is important to note how files are added to your project. When adding files through Xcode, be sure to check the box "Create folder links for any aded folders" and NOT "Create groups for any added folders."

link

+1
source

Source: https://habr.com/ru/post/1260361/


All Articles