Iphone uiwebview download full page with CSS and images

My app has uiwebview which loads the url. I use the following line to save an HTML page loaded locally in order to be able to view it offline.

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

The only problem is that the HTML document is saved, I also want to save the images and CSS together with the HTML so that the user can see the page as if it were online.

just like “keep the web page complete” or something like that we used in browsers.

appreciate your help.

+6
iphone download uiwebview webpage complete
source share
2 answers

There is no easy way. Regex HTML with RegexKitLite ( http://regexkit.sourceforge.net/RegexKitLite/index.html ) and hook all the URLs .jpg, .gif, .png and .css and. js and all you need.

alternately call:

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

or something like that, I'm not javascript whiz ... and then figure out what is coming back;)

Unfortunately. This is a pain in the rear.

edit:

Save all img to iphone, also save html file. When you want to reload the page, load the html from the file to a line and then use

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

to load an HTML string. baseURL is used to indicate the directory or site on which the web view will represent that the html string you are passing is located. All URLs will apply to this.

Please note, of course, that this will not work very well for absolute URLs, only for relative ones. Thus, in your html file there will be monkeys:

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

until this is ok:

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

Again, this whole solution is dirty. You can take a peek at the pre-existing open source recursive html spider. I think wget does what you want, but I doubt that it can be compiled for the iPhone without any problems.

+4
source share

I did not have time to check, but ASIWebPageRequest is very promising. It says that it can "Store the entire web page on one line or with each external resource in a separate file indicated on the page"

ASIWebPageRequest 1

I will use it in one of my projects, and then update the stream.

Gonso

0
source share

All Articles