将包含图像、javascript 和 css 的 html 页面保存在 iphone 应用程序的一个文件夹中 [英] save html page with the images,javascript and css in one folder in iphone application

查看:47
本文介绍了将包含图像、javascript 和 css 的 html 页面保存在 iphone 应用程序的一个文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将网页保存在包含图像、css 和 javascripts 等的文档目录中.

I want to save webpage in document directory with the images,css and javascripts etc..

我已经完成了下面的代码:但它只生成了 html 文件..

I have done below code : But it only generates the html file..

NSURL *url = [NSURL URLWithString:@"http://www.apple.com"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];

欢迎提出任何想法或建议.

Any idea or suggestion is most welcome.

谢谢

推荐答案

ASIHTTPRequest 项目有一个名为 ASIWebPageRequest 的类旨在完全满足您的需求.如果您可以为项目添加额外的依赖项,那么我认为这对您来说是一个很好的解决方案:ASIWebPageRequest.

The ASIHTTPRequest project has a class called ASIWebPageRequest which is designed to do exactly what you want. If you're okay with adding an additional dependency to your project then I think it's a good solution for you: ASIWebPageRequest.

在上面我喜欢的页面上有一些很好的示例说明如何使用它,但为了完整起见,我将在此处包含其中一个示例:

On the page I liked above there are some good examples of how to use it but I'll include one of them here for completeness:

- (IBAction)loadURL:(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
   [[self request] setDownloadDestinationPath:
      [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self request]]];

   [[self request] startAsynchronous];
}

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

这篇关于将包含图像、javascript 和 css 的 html 页面保存在 iphone 应用程序的一个文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆