删除wkwebview目标中的缓存c

时间:2014-11-24 12:49:32

标签: objective-c ios8 wkwebview

我需要删除wkwebview中的缓存。我正在使用下面的代码,但没有成功。

 [[NSURLCache sharedURLCache]removeCachedResponseForRequest:request];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [[NSURLCache sharedURLCache] setDiskCapacity:0];
    [[NSURLCache sharedURLCache] setMemoryCapacity:0];

3 个答案:

答案 0 :(得分:1)

, .; )!

答案 1 :(得分:0)

WKWebView清除缓存iOS 9.0 +

[[WKWebsiteDataStore defaultDataStore] fetchDataRecordsOfTypes:WKWebsiteDataStore.allWebsiteDataTypes completionHandler:^(NSArray<WKWebsiteDataRecord *> * _Nonnull records) {
    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:WKWebsiteDataStore.allWebsiteDataTypes forDataRecords:records completionHandler:^{
       CACHE_CLEARED_MSG;
    }];
}];

答案 2 :(得分:-1)

尝试加载应用程序时首先设置缓存。

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    int cacheSizeMemory = 4*1024*1024; // 4MB
    int cacheSizeDisk = 32*1024*1024; // 32MB
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
    [NSURLCache setSharedURLCache:sharedCache];

}

正确配置后,当需要清除缓存时(例如在applicationDidReceiveMemoryWarning或关闭UIWebView时),只需调用:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

另一件事是你可能想尝试清除cookieStorage。我知道这将重置您登录的网站等。希望这可以帮助。

NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
    [storage deleteCookie:cookie];
}