Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

html - iphone uiwebview download complete page with CSS and Images

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...