gpt4 book ai didi

objective-c - 将包含图像、javascript 和 css 的 html 页面保存在 iphone 应用程序的一个文件夹中

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:55 24 4
gpt4 key购买 nike

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

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

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

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

谢谢

最佳答案

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

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

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

关于objective-c - 将包含图像、javascript 和 css 的 html 页面保存在 iphone 应用程序的一个文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8165973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com