views:

367

answers:

1

Hello,

in my app there's a uiwebview that loads a url.. i use 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.

+1  A: 

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"&gt;

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.

Kenny Winker
Thanks for the quick replyi know its a pain :Sbut i still don't get it..if i got all IMGs tag then what?-how do i place them to be viewed offline?try explaining more if u know ...
zanque
added more details.
Kenny Winker
i think i get the images part.. but how do u get CSS files urls with javascriptbecause the tag name is not of course CSSAgain Thank you!
zanque
can you not do document.getElementsByTagName('link')?
Kenny Winker