【问题标题】:UIWebview not saving the resources along with the html file [duplicate]UIWebview没有将资源与html文件一起保存[重复]
【发布时间】:2012-12-25 19:25:11
【问题描述】:

可能重复:
how to cache a whole web page with images in 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 文件一起保存?

【问题讨论】:

    标签: iphone ios xcode cocoa-touch uiwebview


    【解决方案1】:

    我使用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
        }
    }
    //--------------------------------------------------------------------
    

    【讨论】:

      【解决方案2】:

      基于此page

      wget --mirror –w 2 –p --HTML-extension –-convert-links –P /home/user/sitecopy/
      

      【讨论】:

      • 标签说 iOS 不是 OSX。你不能在 iPhone 上运行 wget!
      猜你喜欢
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      相关资源
      最近更新 更多