UIwebview不会清除iOS 7中的缓存

时间:2014-06-13 14:38:53

标签: objective-c webview uiwebview uiwebviewdelegate nsurlcache

我正在开发一个应用程序,其中很少有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仍在维护已下载的第一组的缓存信息,而不是清除它。

此外,当我在视图中加载我的viewcontroller时,我会调用此代码

  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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我的观察是UIwebview缓存了显示文件的整个路径,甚至在从doc目录中删除这些文件之后,我认为webview很少引用旧版本。

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

  • 您可以从服务器下载文件夹中的资源。

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

在实施上述逻辑后,我的应用效果很好。

答案 2 :(得分:0)

删除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");
    }
}