0

重複の可能性:
iOS で画像を含む Web ページ全体をキャッシュする方法

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を使用して Web ページをキャッシュします。かなり何でもあります (画像、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

このページに基づく:

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