【问题标题】:UIwebview does not clear cache in iOS 7UIwebview 在 iOS 7 中不清除缓存
【发布时间】:2014-06-13 14:38:52
【问题描述】:

我正在开发一个应用程序,其中很少从服务器下载 HTML、CSS 文件并像这样加载到 webview 中

  [[NSURLCache sharedURLCache] removeAllCachedResponses];
  NSURL *url = [NSURL fileURLWithPath:htmlfile];
    htmlLoadRequest = [NSURLRequest requestWithURL:url];
    [self.objwebview loadRequest:htmlLoadRequest];

在 webview 委托方法中我有这段代码

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ 

if(request.cachePolicy != NSURLRequestReloadIgnoringLocalCacheData) {
    NSURLRequest* noCacheRequest = [[NSURLRequest alloc] initWithURL:request.URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:request.timeoutInterval];
    [webView loadRequest:noCacheRequest];

} // further code to do other stuff

在 viewWillDisappear 方法中我有这段代码

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];
[self.objwebview stopLoading];
[self.objwebview setDelegate:nil];

if (htmlLoadRequest) {
    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:htmlLoadRequest];
}
[[NSURLCache sharedURLCache] removeAllCachedResponses];

NSString *htmlfile = [HTML_SERVER_FILES stringByAppendingPathComponent:@"CoverPage.html"];

self.objwebview = nil;

}

我面临问题的地方是,当我第一次下载文件时,一切正常,但是当我更新我的几个 html 文件然后从服务器下载它们时,在这种情况下,我仍然会发生什么查看第一个 html 文件及其图像。

这意味着 webview 的某个地方仍在维护已下载的第一组缓存信息并且没有清除它。

另外,当我的视图控制器加载到视图中时,会出现我调用此代码的方法

  NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
sharedCache = nil;

但什么也没发生,我仍然看到最初从服务器下载的相同的旧 HTML 文件。

查看更新集的唯一方法是杀死应用程序,然后再次启动它,我发现不应该这样做,因为它会创建一个错误的用户 exp。所以请帮我解决这个问题。

更新:我经历了这个URL where it says that its a bug in iOS 7

【问题讨论】:

    标签: objective-c webview uiwebview uiwebviewdelegate nsurlcache


    【解决方案1】:

    UIWebview 可能正在维护路径历史记录,并将从中加载项目。您需要做的是每次以这种方式同步应用程序时将文件夹的名称更改为某个 GUID,webview 将有一个新路径来加载其内容。

    【讨论】:

      【解决方案2】:

      我的观察是 UIwebview 缓存了它显示文件的整个路径,即使在从 doc 目录中删除这些文件之后,在我看来,webview 对旧集的引用很少。

      所以我所做的是我将保存资源文件的文件夹的名称更改为某个 GUID,因此每次我从服务器下载资源集时都会提供一个新的 fileURL(您的 doc 目录 / someGUID)由于 webview 可以加载从服务器下载的新资源集,所以我这样做了

      • 您将资源下载到服务器的文件夹中。

      • 每次下载资源时,下载文件的文件夹的名称必须是某个 GUID,您可以为此使用 NSUUID 类(当然更不用说您需要将该 UUID 存储在一些用户默认值或 plist,以便您可以进一步使用它来提供文件的整个路径)

      在实现上述逻辑后,我的应用运行良好。

      【讨论】:

        【解决方案3】:

        removeAllCachedReponses 和 cookie 删除后,删除 {Bundle Identifier} 文件夹下的所有 .db 文件对我有用。

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
        
            // clear cache
            [[NSURLCache sharedURLCache] removeAllCachedResponses];
            // clear cookies
            for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
                NSLog(@"CLEAR DOMAIN:%@", [cookie domain]);
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
            }
            // remove all db files since removeAllCachedResponses does not work all the time.
            [self removeCacheFiles];
        
            return YES;
        }
        -(void) removeCacheFiles
        {
            NSString *cacheDir=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *nsurlDir = [[NSString alloc] initWithFormat:@"%@/com.company.SampleApp", cacheDir];
            NSFileManager  *manager = [NSFileManager defaultManager];
        
        
            // grab all the files in the documents dir
            NSArray *allFiles = [manager contentsOfDirectoryAtPath:nsurlDir error:nil];
        
            // filter the array for only sqlite files
            NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.db'"];
            NSArray *dbFiles = [allFiles filteredArrayUsingPredicate:fltr];
        
            // use fast enumeration to iterate the array and delete the files
            for (NSString *dbFile in dbFiles)
            {
                NSError *error = nil;
                [manager removeItemAtPath:[nsurlDir stringByAppendingPathComponent:dbFile] error:&error];
                if (error != nil) {
                    NSLog(@"Error:%@", [error description]);
                } else {
                    NSLog(@"DB FILE Cleared: %@", dbFile);
                }
            }
            NSError *error = nil;
            [manager removeItemAtPath:[nsurlDir stringByAppendingString:@"/nsurlcache"] error:&error];
            if (error != nil) {
                NSLog(@"Error:%@", [error description]);
            } else {
                NSLog(@"Dir Cleared");
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-09-09
          • 2011-07-25
          • 2015-04-13
          • 2015-03-22
          • 2013-02-09
          • 2011-02-01
          • 2013-07-24
          • 2012-05-25
          相关资源
          最近更新 更多