How to download a web page with it's inner resoures like images,css in iOS?

若如初见. 提交于 2019-12-21 23:47:57

问题


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!


回答1:


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;
...



回答2:


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



来源:https://stackoverflow.com/questions/15965180/how-to-download-a-web-page-with-its-inner-resoures-like-images-css-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!