NSFilemanager上传新文件,但UIWebView显示缓存版本

时间:2012-12-14 21:32:11

标签: ios caching uiwebview nsfilemanager

我正在构建的应用需要定期检查已知位置以获取更新的文件。它恰好是PDF。如果x时间已过,则应用程序需要上载文件的新副本。以下代码段有效。它下载文件。但该应用程序显示的是PDF的缓存版本而不是新版本。我通过查看应用程序包确认了这一点。运行此代码后,Documents目录中肯定有一个新文件。但是在bundle的tmp / DiskImageCache- [随机乱码字符串]中有一个旧版PDF的副本 - 这就是我的UIWebView显示的内容。

我搜索了NSFileManager文档,当然还有风暴,但是我找不到让应用程序显示新上传而不是PDF的缓存版本的方法。

非常感谢你能解决这个问题。

   -(void) checkFile:(NSString *)url andSaveTo:(NSString __autoreleasing *)filename {

    NSFileManager *manager = [NSFileManager defaultManager];
    NSError *error = nil;      
    NSDictionary *attributes = nil;

    if ([manager fileExistsAtPath:filename]) {

       attributes = [manager attributesOfItemAtPath:filename error:nil];

        double updateInterval = [[attributes fileCreationDate] timeIntervalSinceNow];
        cacheInterval = CacheInterval;

        if (ABS(updateInterval) > CacheInterval) {
         [self downloadFile:url andSaveTo:fileName];
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你总是可以通过在你的网址上附加一个随机数作为参数来阻止缓存

ex:address / yourfilehere.pdf?rand = 0323094230948203984 并且每次给你的长随机参数一个新的随机生成的数字