【问题标题】:Xcode UIWEBVIEW download , save HTML file and showXcode UIWEBVIEW 下载,保存 HTML 文件并显示
【发布时间】:2012-02-21 17:26:03
【问题描述】:

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

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

【问题讨论】:

    标签: iphone xcode ipad uiwebview


    【解决方案1】:

    几周以来我一直想写这篇文章,这个问题终于让我这么做了。见Drop-in offline caching for UIWebView (and NSURLProtocol)

    根据我的经验,简短的回答是,最简单的解决方案是使用NSURLProtocol 拦截NSURLConnection 请求(包括UIWebView 发出的请求)并将结果写入磁盘。如果请求是在您离线时发出的,只需从磁盘读取响应并重播即可。这对应用程序的其余部分非常透明,并且避免了与子类化NSURLCache 相关的各种极端情况。 (有关如何继承 NSURLCache 的更多信息,请参阅 Cocoa With Love 上的 Substituting local data for remote UIWebView requestsAFCache。)

    我的NSURLProtocol 方法非常简单,不是通用的缓存解决方案。它只是为了在您离线时为UIWebView 提供帮助。不过对于这一点,我认为它很好地解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多