iphone uiwebview 下载带有 CSS 和图像的完整页面 [英] iphone uiwebview download complete page with CSS and Images

查看:13
本文介绍了iphone uiwebview 下载带有 CSS 和图像的完整页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中有一个加载 URL 的 uiwebview.我正在使用以下行保存本地加载的页面的 HTML 以便能够离线查看:

In my app there's a uiwebview that loads a URL. I am using the following line to save the HTML of the page loaded locally to be able to view it offline:

NSString* html=[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"]

问题是只有文档的 HTML 被保存.我还想将图像和 CSS 与 HTML 一起保存,以便用户看到页面就像他们在线一样.

The problem is that only the HTML of the document gets saved. I want to save also the images and the CSS along with the HTML so that the user see the page as if they are online.

就像保存网页完成"一样或者类似的东西,我们在浏览器中已经习惯了.

Just like "save web page complete" or something like that, that we're used to in the browsers.

推荐答案

没有简单的方法.使用 RegexKitLite (http://regexkit.sourceforge.net/RegexKitLite/index.html) 并将所有 url 抓取到 .jpg、.gif、.png、.css 和 .js 以及您需要的任何其他文件.

There is no easy way. Regex the HTML using RegexKitLite (http://regexkit.sourceforge.net/RegexKitLite/index.html) and snag all the urls to .jpg,.gif,.png, and .css and .js and whatever all else you need.

或者,调用:

NSString* imgUrls=[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('img')"]

或类似的东西,我不是 javascript 高手……然后处理所有返回的内容 ;)

or something like that, I'm no javascript whizz... and then deal with whatever all that returns ;)

对不起.这是一种痛苦.

Sorry. It's a pain in the rearheinie.

将所有 img 保存在 iphone 上,同时保存 html 文件.当你想重新加载页面时,将文件中的html加载到字符串中,然后使用

Save all the img's on the iphone, also save the html file. When you want to reload the page, load the html from a file into a string, and then use

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

加载 HTML 字符串.baseURL 用于指定 webview 将想象的 html 字符串所在的目录或站点.所有 URL 都将与此相关.

to load the HTML string. baseURL is used to specify the directory or site the webview will imagine the html string you hand it is located. All URLS will be relative to that.

请注意,当然这对于绝对 URL 效果不佳,仅适用于相对 URL.所以,在你的 html 文件中,这会搞砸:

Note, of course that this will not work very well for absolute URLs, only for relative ones. So this, in your html file, will monkey things up:

<img src="http://google.com/f/r/i/g/img.gif">

虽然这没问题:

<img src="f/r/i/g/img.gif">

同样,这整个解决方案很糟糕.您可能会研究一个预先存在的开源递归 html 蜘蛛.我认为 wget 可以满足您的需求,但我怀疑它是否可以为 iPhone 编译而不会遇到很多麻烦.

Again, this whole solution is mucky. You might look into a pre-existing open source recursive html spider. I think wget does what you want, but I doubt it can be compiled for iPhone without a -lot- of hassle.

这篇关于iphone uiwebview 下载带有 CSS 和图像的完整页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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