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!

4

2 回答 2

1

我曾经遇到过同样的问题,我使用ASIWebPageRequest解决了它。当时它已经很旧了,但仍然有效。

我写了一个方法(基于示例方法)来下载文件夹中的网页。您需要对其进行修改以使其正常工作。

- (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];

}

我也使用了 ASIownloadCache。

...
    ASIDownloadCache *localCache = [[ASIDownloadCache alloc] init];
    self.theCache = localCache;
...
于 2013-04-16T18:56:05.047 回答
0

您可以实现一个自定义 NSUrlCache 并保存所有内容。使用 NSUrlRequest 下载的所有内容都将使用 NSUrlCache。UIWebView 所做的一切都使用 NSUrlRequest。如果您需要样本,请查看https://github.com/evermeer/EVURLCache

于 2013-04-12T07:17:57.547 回答