NSFilemanager 上传新文件,但 UIWebView 显示缓存版本 [英] NSFilemanager uploads new file, but UIWebView shows cached version
问题描述
我正在构建的应用程序需要定期检查更新文件的已知位置.它恰好是一个PDF.如果经过 x 时间,应用程序需要上传文件的新副本.以下代码片段有效.它下载文件.但该应用程序会显示 PDF 的缓存版本,而不是新版本.我通过查看应用程序包确认了这一点.这段代码运行后,Documents目录下肯定有一个新文件.但是在捆绑包的 tmp/DiskImageCache-[随机乱码字符串] 中有一份旧版 PDF 的副本 - 这就是我的 UIWebView 显示的内容.
An app I'm building needs to periodically check a known location for an updated file. It happens to be a PDF. If x amount of time has passed, the app needs to upload a new copy of the file. The following code snippet works. It downloads the file. But the app displays a cached version of the PDF instead of the new one. I confirmed this by looking in the app bundle. After this code runs, there is definitely a new file in the Documents directory. But in the bundle's tmp/DiskImageCache-[random gibberish string] there is a copy of the old version of the PDF - and that is what is being displayed by my UIWebView.
我搜索了 NSFileManager 文档,当然,也遇到了麻烦,但我一直无法找到让应用程序显示新上传而不是缓存版本的 PDF 的方法.
I searched the NSFileManager docs and of course, schmoogled up a storm, but I have not been able to find a way to get the app to show the new upload instead of the cached version of the PDF.
非常感谢您对这个问题提供的任何帮助.
Thanks a ton for any assistance you can render with this problem.
-(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];
}
}
}
推荐答案
你总是可以通过在你的 url 中附加一个随机数作为参数来阻止缓存
you can always thwart caching by appending a random number to your url as a parameter
例如:地址/yourfilehere.pdf?rand=0323094230948203984并每次给你的长随机参数一个新的随机生成的数字
ex: address/yourfilehere.pdf?rand=0323094230948203984 and give your long random parameter a new randomly generated number each time
这篇关于NSFilemanager 上传新文件,但 UIWebView 显示缓存版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!