0

Possible Duplicate:
how to cache a whole web page with images in iOS

I have downloaded an html page from an url and I am trying to display it offline using the UIWebView. When I am doing this, html page gets saved but the images and javascript files that comes with the html page does not.

My code is :

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

Can anybody tell me how do I save the resources that comes with my html file along with the html file?

Community
  • 1
  • 1
JTN
  • 229
  • 5
  • 13

2 Answers2

0

I use ASIWebPageRequest to cache webpages. You will have quite anything (images, javascript, etc). See my code.

in your header file:

@property (retain, nonatomic)  ASIWebPageRequest *aSIRequest;

in your implementation file:

//--------------------------------------------------------------------
-(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
    }
}
//--------------------------------------------------------------------
John Smith
  • 2,012
  • 1
  • 21
  • 33
-1

Based on this page:

wget --mirror –w 2 –p --HTML-extension –-convert-links –P /home/user/sitecopy/
Community
  • 1
  • 1
Miles Alden
  • 1,542
  • 1
  • 11
  • 21