0

可能重复:
如何在 iOS 中缓存带有图像的整个网页

我已经从 url 下载了一个 html 页面,我正在尝试使用UIWebView. 当我这样做时,html 页面被保存,但 html 页面附带的图像和 javascript 文件没有。

我的代码是:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"form.html"];


NSURL *url = [NSURL URLWithString:@"http://222.2.2.45/forms/form.html"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];


[_web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

谁能告诉我如何将我的 html 文件附带的资源与 html 文件一起保存?

4

2 回答 2

0

我使用ASIWebPageRequest来缓存网页。您将拥有相当多的东西(图像、javascript 等)。看我的代码。

在你的头文件中:

@property (retain, nonatomic)  ASIWebPageRequest *aSIRequest;

在您的实现文件中:

//--------------------------------------------------------------------
-(void) loadContent {
    //use ASIWebPageRequest to load content from web and cache, or to load already cached content.
    //the content will get downloaded each time the internet will be reachable, so
    //the webpage cache will will always be up to date

    NSURL *url = [NSURL URLWithString:url_to_your_webpage];

    [self setASIRequest:[ASIWebPageRequest requestWithURL:url]];
    [self.aSIRequest setDelegate:self];
    [self.aSIRequest setDidFailSelector:@selector(webPageFetchFailed:)];
    [self.aSIRequest setDidFinishSelector:@selector(webPageFetchSucceeded:)];
    [self.aSIRequest setUrlReplacementMode:ASIReplaceExternalResourcesWithData];
    [self.aSIRequest setDownloadCache:[ASIDownloadCache sharedCache]];
    [self.aSIRequest setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
    self.aSIRequest.cachePolicy = ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy;
    [self.aSIRequest setDownloadDestinationPath:
     [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:[self aSIRequest]]];
    [self.aSIRequest startAsynchronous];
}
//--------------------------------------------------------------------
- (void)webPageFetchFailed:(ASIHTTPRequest *)theRequest{
    //this will generally get called if there is no data cached and no internet connection
    //or other damn reason.
    //let the user retry loading the content

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error!" message:@"Failed to load the content." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Retry", nil];
    [alert show];
    [alert release];
}
//--------------------------------------------------------------------
- (void)webPageFetchSucceeded:(ASIHTTPRequest *)theRequest{
    //load the webview with the downloaded or cashed content
    NSString *response = [NSString stringWithContentsOfFile: [theRequest downloadDestinationPath] encoding:[self.aSIRequest responseEncoding] error:nil];
    [youWebView loadHTMLString:response baseURL:[self.aSIRequest url]];
}
//--------------------------------------------------------------------
- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 1){//retry button tapped
        [self performSelector:@selector(loadContent) withObject:nil afterDelay:0.2];
    }
    else{//cancel button tapped
        //do nothing.
        //alternatively, you could lead the user to home screen or perform any else action
    }
}
//--------------------------------------------------------------------
于 2013-01-11T08:48:05.330 回答
-1

Based on this page:

wget --mirror –w 2 –p --HTML-extension –-convert-links –P /home/user/sitecopy/
于 2013-01-11T08:41:00.887 回答