如何下载一个网页的内部资源,如图像,在iOS的CSS?
我试图下载指定的网页与他们的内部页面资源到磁盘。 当我发送一个NSURLRequest,只有最初的URL将是请求,但资源作为图像,CSS,JS不会被下载。
有人build议使用caching,但我不想caching在uiwebview浏览的每个网页,但只有我指定的网页。例如,当我input一个url到uiwebview地址栏然后点击“保存”button,该页面将被保存完整,但在web视图中的其他浏览将不会被自动caching。
有没有办法下载整个网页? 非常感谢你!
我跑了同样的问题一次,我解决了它使用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];
}
我也使用ASIDownloadCache。
... ASIDownloadCache *localCache = [[ASIDownloadCache alloc] init]; self.theCache = localCache; ...
你可以实现一个custum NSUrlCache并保存一切。 所有使用NSUrlRequest下载的将使用NSUrlCache。 UIWebView所做的一切都使用NSUrlRequest。 如果您需要示例,请查看https://github.com/evermeer/EVURLCache