How to remove cache in WKWebview?

Sravan picture Sravan · Nov 24, 2014 · Viewed 54.8k times · Source

Any one worked with WKWebview and tried to clear cache ? If yes, how to do? any example ?

P.S. : Normal NSURLCache is not working.

Answer

Shingo Fukuyama picture Shingo Fukuyama · Sep 10, 2015

In iOS 9

// Optional data
NSSet *websiteDataTypes
= [NSSet setWithArray:@[
                        WKWebsiteDataTypeDiskCache,
                        //WKWebsiteDataTypeOfflineWebApplicationCache,
                        WKWebsiteDataTypeMemoryCache,
                        //WKWebsiteDataTypeLocalStorage,
                        //WKWebsiteDataTypeCookies,
                        //WKWebsiteDataTypeSessionStorage,
                        //WKWebsiteDataTypeIndexedDBDatabases,
                        //WKWebsiteDataTypeWebSQLDatabases,
                        //WKWebsiteDataTypeFetchCache, //(iOS 11.3, *)
                        //WKWebsiteDataTypeServiceWorkerRegistrations, //(iOS 11.3, *)
                        ]];
// All kinds of data
// NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
// Date from
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
// Execute
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
   // Done
}];