UIWebView 为离线视图复制缓存数据

UIWebView Copy Cache Data For Offline View

我想将缓存数据复制到数据库中(sqlite/coreData),这样我就可以离线(没有互联网)查看,就像“Safari 中的阅读列表”一样。我在 Xcode UIWEBVIEW download , save HTML file and show 阅读了代码 以下是:-

- (void)viewDidLoad
{

    NSURL *url = [NSURL URLWithString:@"http://livedemo00.template-help.com/wt_37587/index.html"]; 


    //[WEBVIEW loadRequest:reqURL];


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

    // Download and write to file

    NSData *urlData = [NSData dataWithContentsOfURL:url];
    [urlData writeToFile:filePath atomically:YES];

    // Load file in UIWebView
    [WEBVIEW loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];      



    [super viewDidLoad];
}

但此代码的问题在于它在离线时不显示图像。

所以我认为将数据从缓存保存到数据库可能是另一种方法option.Can任何人都可以分享一些示例代码来将网站内容连同图像一起复制到数据库。

提前致谢

您可以使用 ASIWebPageRequest ,给出的示例 here

如果可达性得到连接

NSCachedURLResponse* response = [[NSURLCache sharedURLCache] 
                                     cachedResponseForRequest:[webView request]];
NSData* data = [response data];

将数据存储在本地并在离线时使用它。