Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 843
Mina Mikhael Avatar asked Nov 12 '09 07:11

Mina Mikhael


1 Answers

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.

like image 191
Kenny Winker Avatar answered Oct 22 '22 10:10

Kenny Winker