无法下载整个html页面 - Objective C / Xcode

时间:2012-06-19 08:28:59

标签: iphone objective-c ios xcode ipad

我使用以下代码行下载并保存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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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]];
}

答案 2 :(得分:0)

- (void)viewDidLoad {
/// js=yourHtmlSring;
 NSString *js;  (.h)
 [self.myWebView loadHTMLString:js baseURL:nil];
 }

//委托

- (void)webViewDidFinishLoad:(UIWebView *)webView {
[myWebView stringByEvaluatingJavaScriptFromString:js];
 }`

答案 3 :(得分:0)

嘿我认为你不能从谷歌下载所有文件只是尝试与任何其他网址。您可以直接将NSData写入您的文件htmlFilePath

[data writeToFile:htmlFilePath atomically:YES];