【问题标题】:UIWebView Copy Cache Data For Offline ViewUIWebView 为离线视图复制缓存数据
【发布时间】:2015-08-14 14:07:24
【问题描述】:

我想将缓存数据复制到数据库(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];
}

但是这段代码的问题是它在离线时不显示图像。

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

提前谢谢你

【问题讨论】:

标签: ios objective-c caching uiwebview


【解决方案1】:

你可以使用 ASIWebPageRequest ,例如here

或者

如果可达性获得连通性

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

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

【讨论】:

    猜你喜欢
    • 2013-10-07
    • 2013-05-25
    • 2013-06-14
    • 2016-12-16
    • 1970-01-01
    • 2013-01-23
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多