如何在UIWebview中的目录(js,images,css)中加载本地html资源? (IOS) [英] How to load local html assets in directories (js, images, css) in a UIWebview? (iOS)
问题描述
具体来说 - 如何在不更改原始HTML(删除目录)的内容的情况下从各自的目录(images /,css /,js /)中加载所有资源?
我使用此代码将index.html加载到UIWebview中:
NSURL * url = [ [NSBundle mainBundle] URLForResource:@indexwithExtension:@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];
然后我的html看起来像这样:
...
< link rel =stylesheettype =text / csshref =css / main.css/>
< script type =text / javascriptsrc =js / classes.js>< / script>
< script type =text / javascriptsrc =js / animate.js>< / script>
等...
如果我删除了js /,css /和images /一切正常,因为iOS认为一切都在我的包的根目录中。但是,这意味着我无法跨项目使用相同的代码,而无需将其全部转储到一个目录中(跛脚)。我的工程师无法应对这种混乱。
提前致谢。
简单:当您将文件夹添加到Xcode时,请确保将它们添加为文件夹引用。
<然后将它们作为文件夹添加到.app包中:
Specifically -- how do you get all the assets to load from their respective directories (images/, css/, js/) without changing the content of the original HTML (removing directories)?
I am using this code to load index.html into the 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 the js/, css/, and images/, everything works because iOS thinks everything is in the root of my bundle. But, that means I cant use the same code across projects without dumping it all into one directory (lame). The engineer in me can't deal with this kind of disorganization.
Thanks in advance.
Easy: when you add the folder(s) to Xcode, make sure you add them as folder references.
They will then be added as folders to the .app bundle:
这篇关于如何在UIWebview中的目录(js,images,css)中加载本地html资源? (IOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!