Xcode UIWEBVIEW下载,保存HTML文件并显示 [英] Xcode UIWEBVIEW download , save HTML file and show

查看:136
本文介绍了Xcode UIWEBVIEW下载,保存HTML文件并显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个页面并在WEBVIEW本地显示,但图像丢失了。只显示图像,当我在线时,在离线模式下它们会丢失。我怎么解决这个问题?这是我到目前为止使用的代码:

I want to download a page and show this in WEBVIEW local, but the images are missing. The images are only shown, when I'm online, in offline mode they are missing. How can I solve this? This is the code I used up until now:

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

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


推荐答案

我一直想要现在写几个星期,这个问题终于让我去做了。请参阅用于UIWebView(和NSURLProtocol)的Drop-in离线缓存

I've been meaning to write this up for a few weeks now, and this question finally got me to do it. See Drop-in offline caching for UIWebView (and NSURLProtocol).

根据我的经验,简短的答案是最简单的解决方案是使用 NSURLProtocol 来拦截 NSURLConnection 请求(包括由 UIWebView 创建的请求)并将结果写入磁盘。如果在您离线时发出请求,只需从磁盘读取响应并重播。这对应用程序的其余部分非常透明,并避免了与子类化 NSURLCache 相关的各种边缘情况。 (有关如何子类 NSURLCache 的更多信息,请参阅在Cocoa With Love上替换本地数据以获取远程UIWebView请求,并 AFCache 。)

The short answer, in my experience, is that the easiest solution is to use an NSURLProtocol to intercept NSURLConnection requests (including the ones made by UIWebView) and write the results to disk. If the request is made when you're offline, just read the response from disk and replay it. This is extremely transparent to the rest of the application, and avoids all kinds of edge-cases related to subclassing NSURLCache. (For more information on how to subclass NSURLCache see Substituting local data for remote UIWebView requests on Cocoa With Love, and AFCache.)

我的 NSURLProtocol 方法非常简单,并不是通用的缓存解决方案。它只是为了在您离线时协助 UIWebView 。但为此,我认为它很好地解决了这个问题。

My NSURLProtocol approach is very simple, and is not a general-purpose caching solution. It's just intended to assist a UIWebView when you're offline. But for that, I think it solves the problem well.

这篇关于Xcode UIWEBVIEW下载,保存HTML文件并显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆