清除WKWebView的缓存
1 OC写法: swift写法再下下面。
2
3 清除WKWebView的缓存,让H5页面一刷新就更新至最新的页面
4
8
9 要区分iOS9.0和8.0两种
10
11 - (void)deleteWebCache {
12
13
14
15 if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
16
17 NSSet *websiteDataTypes
18
19 = [NSSet setWithArray:@[
20
21 WKWebsiteDataTypeDiskCache,
22
23 //WKWebsiteDataTypeOfflineWebApplicationCache,
24
25 WKWebsiteDataTypeMemoryCache,
26
27 //WKWebsiteDataTypeLocalStorage,
28
29 //WKWebsiteDataTypeCookies,
30
31 //WKWebsiteDataTypeSessionStorage,
32
33 //WKWebsiteDataTypeIndexedDBDatabases,
34
35 //WKWebsiteDataTypeWebSQLDatabases
36
37 ]];
38
39 //// All kinds of data
40
41 //NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
42
43 //// Date from
44
45 NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
46
47 //// Execute
48
49 [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
50
51 // Done
52
53 }];
54
55
56
57 } else {
58
59
60
61 NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
62
63 NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
64
65 NSError *errors;
66
67 [[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
68
69
70
71 }
72
73 }
74
75
76
77
78
79 swift:
80
81 // MARK: - 清空缓存
82
83 func ClearCache() {
84
85
86
87 let dateFrom: NSDate = NSDate.init(timeIntervalSince1970: 0)
88
89
90
91 if #available(iOS 9.0, *) {
92
93 let websiteDataTypes: NSSet = WKWebsiteDataStore.allWebsiteDataTypes()
94
95 WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes as! Set<String>, modifiedSince: dateFrom) {
96
97 print("清空缓存完成")
98
99 }
100
101 } else {
102
103 let libraryPath = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]
104
105 let cookiesFolderPath = libraryPath.stringByAppendingString("/Cookies")
106
107 let errors: NSError
108
109 try? NSFileManager.defaultManager().removeItemAtPath(cookiesFolderPath)
110
111 }
112
113 }
让明天,不后悔今天的所作所为
posted on 2016-10-31 11:41 Sinner_Yun 阅读(2564) 评论(0) 编辑 收藏 举报