2011-11-17 76 views
2

我想保存與圖片,CSS和JavaScript的等文檔目錄網頁中的圖片,JavaScript和CSS一個文件夾中的iPhone應用程序..保存的HTML頁面

我做了下面的代碼:但是,只有它生成html文件..

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

任何想法或建議是最受歡迎的。

感謝

回答

2

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]]; 
} 
+0

感謝Srikar :) – NSS

+0

感謝upvotes! –