NSFilemanager uploads new file, but UIWebView shows cached version

旧巷老猫 提交于 2020-01-06 15:41:09

问题


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.

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

回答1:


you can always thwart caching by appending a random number to your url as a parameter

ex: address/yourfilehere.pdf?rand=0323094230948203984 and give your long random parameter a new randomly generated number each time



来源:https://stackoverflow.com/questions/13886540/nsfilemanager-uploads-new-file-but-uiwebview-shows-cached-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!