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

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

问题描述

我到处找这个,找不到任何东西.出于一些有趣的原因,我基本上需要在我的 iPhone 中存储整个网站.无论如何我无法弄清楚如何在我的 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/javascript" src="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 还是绝对 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.

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

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天全站免登陆