1

我正在使用以下代码行下载并保存 html 页面 ::

NSURL *goo = [[NSURL alloc] initWithString:@"http://www.google.com"];
NSData *data = [[NSData alloc] initWithContentsOfURL:goo]; 
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; //Remove the autorelease if using ARC

NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@", documentsDirectory);
NSString *htmlFilePath = [documentsDirectory stringByAppendingPathComponent:@"file.html"];
[html writeToFile:htmlFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

下载并保存后,我需要重新使用它,即上传它。但是,我无法下载css和图像文件以及html页面,即重新上传它时..我没有得到应该在谷歌主页上显示的图像..

有人可以帮我解决问题吗?谢谢并恭祝安康。

4

4 回答 4

0

嘿,我认为您无法从 google 下载所有文件,只需尝试使用任何其他 url 即可。您可以直接将其写入NSData您的文件htmlFilePath

[data writeToFile:htmlFilePath atomically:YES];
于 2014-03-11T06:12:56.283 回答
0

使用ASIWebPageRequest将解决问题:

  - (void)downloadHtml:(NSURL *)url
 {
   // Assume request is a property of our controller
   // First, we'll cancel any in-progress page load
   [[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];

   // 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] setDownloadCache:[ASIDownloadCache sharedCache]];

   // 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
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   [[self request] setDownloadDestinationPath:documentsDirectory] // downloaded path
   //[[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self request]]]; use this instead of documentsDirectory if u want to cache the page

   [[self request] startAsynchronous];
}

//These are delegates methods:
- (void)webPageFetchFailed:(ASIHTTPRequest *)theRequest
{
  // Obviously you should handle the error properly...
  NSLog(@"%@",[theRequest error]);
}

- (void)webPageFetchSucceeded:(ASIHTTPRequest *)theRequest
{
   NSString *response = [NSString stringWithContentsOfFile:
   [theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil];
   // Note we're setting the baseURL to the url of the page we downloaded. This is important!
  [webView loadHTMLString:response baseURL:[request url]];
}
于 2012-06-19T08:59:54.100 回答
0
- (void)viewDidLoad {
/// js=yourHtmlSring;
 NSString *js;  (.h)
 [self.myWebView loadHTMLString:js baseURL:nil];
 }

//代表

- (void)webViewDidFinishLoad:(UIWebView *)webView {
[myWebView stringByEvaluatingJavaScriptFromString:js];
 }`
于 2012-06-19T10:06:29.133 回答
0

正在下载的数据正是 Web 服务器返回的内容 - 纯 html。如果您需要来自内部的资源 - images/sounds/flash/css/javascripts/etc.. 您已解析此 html 并下载所有其他资源.. 您的 html 还可能包含这些资源的完整路径,因此您可能需要更改他们的网址是相对的(如果您想离线显示或上传到另一台服务器)。解析可以使用正则表达式或其他一些可以下载整个网页的第 3 方解析器或库来完成...
您可以看看ASIWebPageRequest,它声称能够下载整个网站,但我没有尝试过这个功能...

于 2012-06-19T08:37:06.353 回答