1

I'm trying to download the specified web pages with their inner page resources to disk. When I send a NSURLRequest, only the initial URL would be request, but the resources as images,css,js would not be download.

Someone suggests to use cache, but I don't want to cache every web page browsed in the uiwebview, but only the one I specified.For example, when I input an url to the uiwebview addressbar then click on the "save" button, the page will be saved entire, but the other browsing in the webview will not be cached automatically.

Does there any way to download the entire webpages? Thank you very much!

2
  • I KNOW ITS A STUPID QUESTION.. BUT YOU ARE DOWNLOADING THE WEBPAGE AND ITS INFORMATION FROM A SPECIFIED SERVER RIGHT? Commented Apr 12, 2013 at 7:14
  • I mean save certain URL to disk with the page resources.Not cache every page, and not from a specified server. Commented Apr 12, 2013 at 7:26

2 Answers 2

1
+50

I ran in the same problem once, and I solved it using ASIWebPageRequest . It was old by the time, but still works.

I wrote a method (based on sample method) for download a webpage in a folder. You need to modify this to get it work.

- (IBAction)loadURL:(NSURL *)url inFolder:(NSString*)folderPath { // Assume request is a property of our controller // First, we'll cancel any in-progress page load [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; [[self request] setDelegate:nil]; [[self request] cancel]; [self setRequest:[ASIWebPageRequest requestWithURL:url]]; [[self request] setDelegate:self]; [[self request] setDidFailSelector:@selector(webPageFetchFailed:)]; [[self request] setDidFinishSelector:@selector(webPageFetchSucceeded:)]; // Tell the request to embed external resources directly in the page [[self request] setUrlReplacementMode:ASIReplaceExternalResourcesWithData]; self.theCache.storagePath = folderPath; // It is strongly recommended you use a download cache with ASIWebPageRequest // When using a cache, external resources are automatically stored in the cache // and can be pulled from the cache on subsequent page loads self.request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy; [[self request] setDownloadCache:self.theCache]; // Ask the download cache for a place to store the cached data // This is the most efficient way for an ASIWebPageRequest to store a web page //[[self request] setDownloadDestinationPath: //[[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self request]]]; [[self request] setDownloadDestinationPath:[self.theCache pathToStoreCachedResponseDataForRequest:[self request]]]; [[self request] startAsynchronous]; 

}

I used ASIDownloadCache too.

... ASIDownloadCache *localCache = [[ASIDownloadCache alloc] init]; self.theCache = localCache; ... 
Sign up to request clarification or add additional context in comments.

5 Comments

Thank You first!I've ever tried the ASIWebPageRequest, but it didn't perform correct always, someone says it's not complete and not mature.How about it in your use?
I used it just for download a web, with its css and images. Nothing more, and works fine. I can't say if it is not mature, but certainly is outdated, you will need to disable ARC for those files to build the project. So if you don't want to use it, maybe you can check the implementation and write your own code (I will try to do it too).
If you're going for that ASIWebPageRequest-based solution I would check this project github.com/pokeb/ProxyingUIWebView by @pokeb (ASIHTTPRequest) to get some inspiration ;-)
ASIWebPageRequest is a true classic
ASIHTTPRequest is the classic, but now is totally replaced by AFNetworking
0

You could implement a custum NSUrlCache and save everything. Everything that is downloaded using a NSUrlRequest will use the NSUrlCache. Everything a UIWebView does uses the NSUrlRequest. If you need a sample, then have a look at https://github.com/evermeer/EVURLCache

5 Comments

Thank you!But I don't want to cache every web page browsed in the uiwebview, but only the one I specified.For example, when I input an url to the uiwebview addressbar then click on the "save" button, the page will be saved entire, but the other browsing in the webview will not be cached automatically.How can I make that?
The moment you press the save button, you should initialize the custom cache and do a refresh of the UIWebView. The moment the webpage is loaded, you should initialize the standard cache. (which disables the custom cache)
If I have two UIWebView in my app, one for the common browsing, another for the webpage saving operation at background.I turn on/off the customed cache will influence the UIWebView for common browsing right?
If they download at the same time, then yes, it will cache both pages. For the page itself you could add an if check in the NSUrlCache. I don't think there is a way to tell from what page the other files (images) were downloaded from.
I tried to add a header flag to the initial request, then check in the cache,if YES cache it, else pass it. But the inner resources (images) are requested by the UIWebView separately,these requests can not be intercepted to modify their header, and they have no feature show which request they belong to,so I have no way to check these subsequent request.I tried to check by the referer, but are not correct always.I can find no more way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.