如何在iOS8中删除WKWebview的缓存? [英] How to delete WKWebview 's Cache in iOS8?
问题描述
如何在iOS8中删除WKWebview的缓存?
对于iOS 9,以下代码正常工作。
How to delete WKWebview 's Cache in iOS8? For iOS 9 Below code was working.
let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeCookies])
let date = NSDate(timeIntervalSince1970: 0)
WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes as! Set<String>, modifiedSince: date, completionHandler:{ })
对于iOS 8,我尝试了以下链接中的解决方案,但未删除缓存。
For iOS 8, I tried solutions in following links, but Cache is not deleted.
http://blogs.candoerz.com/question/128462/how-to-delete-wkwebview-cookies.aspx
http://atmarkplant.com/ios-wkwebview-tips/
感谢您的帮助。
推荐答案
我正在开发适用于iOS的浏览器,并希望分享我们在该问题上的经验。
I am working on browser for iOS and wanna to share our experience in that question.
首先,浏览器中的所有Web视图通过单个processPool相互连接。这导致在所有webView之间共享cookie。
为此,我们为WKWebViewConfiguration设置了相同的processPool,并将其传递给新创建的webView:
First of all, all webviews in our browser connected between each other via single processPool. It leads to sharing cookies between all webViews. To do this, we set same processPool to WKWebViewConfiguration, that is passed to newly created webView:
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
configuration.processPool = self.processPool;
WKWebView* webView = [[WKWebView alloc] initWithFrame:frame
configuration:configuration];
第二,数据删除过程如下:
Secondly, the process of data removing looks like this:
-
删除所有创建的webView
Remove all created webViews
删除具有缓存/ cookie的目录
Remove directories with cache/cookies
创建新进程池
使用新进程池重新创建webView
Recreate webViews with new process pool
如果您有1个webView,则整个过程应如下所示:
If you have 1 webView, the whole process should look like this:
- (void)clearWebViewData {
[self.webView removeFromSuperview];
self.webView = nil;
NSFileManager* fileManager = [NSFileManager defaultManager];
NSURL* libraryURL = [fileManager URLForDirectory:NSLibraryDirectory
inDomain:NSUserDomainMask
appropriateForURL:NULL
create:NO
error:NULL];
NSURL* cookiesURL = [libraryURL URLByAppendingPathComponent:@"Cookies"
isDirectory:YES];
[fileManager removeItemAtURL:cookiesURL error:nil];
NSURL* webKitDataURL = [libraryURL URLByAppendingPathComponent:@"WebKit" isDirectory:YES];
NSURL* websiteDataURL = [webKitDataURL URLByAppendingPathComponent:@"WebsiteData" isDirectory:YES];
NSURL* localStorageURL = [websiteDataURL URLByAppendingPathComponent:@"LocalStorage" isDirectory:YES];
NSURL* webSQLStorageURL = [websiteDataURL URLByAppendingPathComponent:@"WebSQL" isDirectory:YES];
NSURL* indexedDBStorageURL = [websiteDataURL URLByAppendingPathComponent:@"IndexedDB" isDirectory:YES];
NSURL* mediaKeyStorageURL = [websiteDataURL URLByAppendingPathComponent:@"MediaKeys" isDirectory:YES];
[fileManager removeItemAtURL:localStorageURL error:nil];
[fileManager removeItemAtURL:webSQLStorageURL error:nil];
[fileManager removeItemAtURL:indexedDBStorageURL error:nil];
[fileManager removeItemAtURL:mediaKeyStorageURL error:nil];
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
configuration.processPool = [[WKProcessPool alloc] init];
self.webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];
[self.webView loadRequest:request];
}
我想引起您的注意,此代码仅在上有效iOS 8.x中的设备。它根本无法在模拟器中使用。
I want to draw your attention, that this code works only on device in iOS 8.x. It doesn't work in simulator at all.
这篇关于如何在iOS8中删除WKWebview的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!