如何在 XCode/iOS 中包含 HTML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18702004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I include HTML files in XCode / iOS?
提问by Christos Hayward
I would like to display HTML files included in an application bundle, but when I go to create a new document, I don't clearly see how to include HTML, except as an empty document.
我想显示包含在应用程序包中的 HTML 文件,但是当我去创建一个新文档时,我不清楚如何包含 HTML,除了作为一个空文档。
What's a correct "<p>Hello, world!</p>" for, ideally, including HTML files as resources?
理想情况下,包含 HTML 文件作为资源的正确“<p>Hello, world!</p>”是什么?
Thanks,
谢谢,
回答by Woodstock
From here: Load resources from relative path using local html in uiwebview
从这里:在 uiwebview 中使用本地 html 从相对路径加载资源
Drag the resource into your xcode project (I dragged a folder named www from my finder window), you will get two options "create groups for any added folders" and "create folders references for any added folders". Select the "create folder references.." option. Use the below given code. It should work like a charm.
将资源拖入您的 xcode 项目(我从查找器窗口中拖出一个名为 www 的文件夹),您将获得两个选项“为任何添加的文件夹创建组”和“为任何添加的文件夹创建文件夹引用”。选择“创建文件夹引用...”选项。使用下面给出的代码。它应该像魅力一样工作。
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
Now all your relative links(like img/.gif, js/.js) in the html should get resolved.
现在 html 中的所有相关链接(如 img/.gif、js/.js)都应该得到解析。
回答by e1985
AFAIK there is no HTML file template and neither decent HTML editing capabilities in Xcode. So I suggest to edit the HTML files with another editor and add them to your project - you can either drag & drop or use right click and "add files to ...".
AFAIK 在 Xcode 中没有 HTML 文件模板,也没有像样的 HTML 编辑功能。所以我建议使用另一个编辑器编辑 HTML 文件并将它们添加到您的项目 - 您可以拖放或使用右键单击并“将文件添加到...”。