UIWebView显示本地存储的网站(HTML,图像,Javascript) [英] UIWebView display locally stored website (HTML, images, Javascript)

查看:100
本文介绍了UIWebView显示本地存储的网站(HTML,图像,Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此无所事事,找不到任何东西。出于某些有趣的原因,我基本上需要将整个网站存储在我的i​​Phone中。无论我无法弄清楚如何在我的UIWebView中正确显示它。

I've looked EVERYWHERE for this, can't find anything. I basically need to store an entire website inside of my iPhone for some interesting reasons. Regardless I can't figure out how to display it correctly in my UIWebView.

编辑:我应该澄清,我可以加载原始的HTML文件,我已经把所有的路径是本地的,除了没有链接。

I should clarify, I can load the original HTML file, and I have chagned all of the pathing to be local, except nothing gets linked in.

这是代码

self.dataDetectorTypes = UIDataDetectorTypeLink;
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self loadRequest:request];

index.html有一堆< script type =text / javascriptsrc =somescript.js>

index.html has a bunch of <script type="text/javascript" src="somescript.js">

没有任何JS代码被执行

None of the JS code gets executed

推荐答案

看起来你正在从你的包中加载HTML。这意味着所有其他文件(.js,.css和任何媒体文件)也需要出现在您的包中。因此,首先要检查的是查看可执行文件的内容,并确保包含js等文件。

Looks like you're loading the HTML from inside your bundle. This means that all the additional files (.js, .css, and any media files) also need to be present in your bundle. So the first thing to check is to look inside the contents of your executable and make sure the js, etc. files are included.

如果看起来很好,接下来要做的就是检查html,js或css文件是否通过相对或绝对URL引用内容。如果Web内容中存在绝对路径引用,则UIWebView将尝试每次都下载该内容,因此只有在您具有网络连接时它才会起作用。如果路径是相对的,那么它将查看捆绑包以查看是否存在这样的文件。

If that looks fine the next thing to check is if the html, js, or css files reference content via relative or absolute URLs. If there's an absolute path reference in the web content then UIWebView is going to try to download that content each time so it'll only work when you have a net connection. If the path is relative then it's going to look in the bundle to see if such a file exists.

当您将html和内容包含到XCode项目文件中时,您可能将文件拖到项目侧栏,并询问是否为任何添加的文件夹递归创建组或为任何添加的文件夹创建文件夹参考。

When you included the html and content into the XCode project file you probably dragged the file(s) over to the project side-bar and were asked whether to "Recursively create groups for any added folders" or to "Create Folder References for any added folders."

默认值是第一个,这意味着XCode在项目中创建一个黄色文件夹,但是当生成输出包时,它将忽略磁盘上的目录层次结构。如果您选择第二个选项,则该文件夹为蓝色,如果您查看输出包,则会看到整个文件夹层次结构已被复制。

The default is the first one which means XCode creates a yellow folder in your project, but it'll ignore the directory hierarchy on disk when time comes to generate the output bundle. If you choose the second option then the folder is blue and if you look in your output bundle you'll see that the whole folder hierarchy has been replicated.

第一个工作对于简单的网页,其中所有内容都在同一文件夹级别,您可以使用上面列出的方法加载它。第二种情况更好,如果您的网页很复杂并引用子文件夹中的内容,在这种情况下您需要从相对路径(例如,'webpages'文件夹)加载网页:

The first works for simple web pages where everything is at the same folder level and you can use the method you list above to load it. The second case works better if your web page is complex and references content in sub-folders in which case you need to load the web pages from a relative path (say, the 'webpages' folder):

NSString *path = [[NSBundle mainBundle] 
                 pathForResource:@"index" ofType:@"html" 
                     inDirectory:@"webpages"];

最后要检查的是html文件中是否有BASE标签。这是一种为页面上的所有链接指定默认地址或目标的方法,但它可以删除webview链接。

The last thing to check for is if there are any BASE tags in the html file. This is a way to specify a default address or target for all links on a page, but it can muck up webview links.

这篇关于UIWebView显示本地存储的网站(HTML,图像,Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆