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

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

问题描述

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

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

问题只是保存的文档的HTML,我还要保存图像和CSS以及HTML,以便用户看到页面,就像他在线。



就像保存网页完成或类似的东西,我们以前在浏览器中。



感谢您的帮助。

解决方案

没有简单的方法。使用RegexKitLite对HTML进行正则表达式( http://regexkit.sourceforge.net/RegexKitLite/index.html ),并将所有URL都标记为.jpg,.gif,.png,.css和.js以及其他任何您需要的内容。



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

或类似的东西,我不是javascript,然后处理所有返回的任何东西) p>

对不起,这是一个痛苦的backheinie。



编辑:



保存所有的img在iphone上,也保存html文件。当您要重新加载页面时,将html从文件加载到字符串中,然后使用

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

加载HTML字符串。 baseURL用于指定webview将会想象您所在的html字符串的目录或站点。所有的URLS都是相对的。



请注意,绝对URL只能用于相对的URL。所以这个,在你的html文件中,将会monkey monkey一下:

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

而这样可以:

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

同样,这整个解决方案是很幸运的。
您可能会查看一个预先存在的开源递归html蜘蛛。我认为wget做你想要的,但是我怀疑它可以编译为iPhone,而没有一点麻烦。


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"]

The problem is only the HTML of the document that 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 he is online.

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

appreciate your help.

解决方案

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.

alternately, call:

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

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.

edit:

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

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.

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">

while this would be ok:

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

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