KWebsiteDataStore
这个类有两个作用
-
1、提取缓存的WKWebsiteDataRecord
-
2、清缓存
关于WKWebsiteDataRecord
提取缓存对应的WKWebsiteDataRecord的Api
/*! @abstract Fetches data records containing the given website data types.
@param dataTypes The website data types to fetch records for.
@param completionHandler A block to invoke when the data records have been fetched.
*/
- (void)fetchDataRecordsOfTypes:(NSSet<NSString *> *)dataTypes completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler;
这个方法回调一个WKWebsiteDataRecord的数组。
WKWebsiteDataRecord
这个类里除了定义了很多缓存的类型
/*! @constant WKWebsiteDataTypeFetchCache On-disk Fetch caches. */
WK_EXTERN NSString * const WKWebsiteDataTypeFetchCache API_AVAILABLE(macosx(10.13.4), ios(11.3));
/*! @constant WKWebsiteDataTypeDiskCache On-disk caches. */
WK_EXTERN NSString * const WKWebsiteDataTypeDiskCache API_AVAILABLE(macosx(10.11), ios(9.0));
/*! @constant WKWebsiteDataTypeMemoryCache In-memory caches. */
WK_EXTERN NSString * const WKWebsiteDataTypeMemoryCache API_AVAILABLE(macosx(10.11), ios(9.0));
...
还有两个属性
/*! @abstract The display name for the data record. This is usually the domain name. */
@property (nonatomic, readonly, copy) NSString *displayName;
/*! @abstract The various types of website data that exist for this data record. */
@property (nonatomic, readonly, copy) NSSet<NSString *> *dataTypes;
displayName,只读的,This is usually the domain name
。
//读取WKWebsiteDataRecord的方法
NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
[self.webView.configuration.websiteDataStore fetchDataRecordsOfTypes:websiteDataTypes completionHandler:^(NSArray<WKWebsiteDataRecord *> * _Nonnull array) {
for (int i = 0; i < array.count; i++) {
WKWebsiteDataRecord *reaord = array[i];
NSLog(@"displayName:%@",reaord.displayName);
}
}];
这里打印了displayName
,如果加载百度页面的话,打印出来”baidu.com“。reaord.dataTypes我没打印,应该是该网页下的缓存类型的集合。
清除缓存
清除date时间之后的缓存的Api
/*! @abstract Removes all website data of the given types that has been modified since the given date.
@param dataTypes The website data types that should be removed.
@param date A date. All website data modified after this date will be removed.
@param completionHandler A block to invoke when the website data has been removed.
*/
- (void)removeDataOfTypes:(NSSet<NSString *> *)dataTypes modifiedSince:(NSDate *)date completionHandler:(void (^)(void))completionHandler;
这里的dataTypes可直接传入[WKWebsiteDataStore allWebsiteDataTypes],清除所有缓存。
清除指定record下的缓存的Api
/*! @abstract Removes website data of the given types for the given data records.
@param dataTypes The website data types that should be removed.
@param dataRecords The website data records to delete website data for.
@param completionHandler A block to invoke when the website data for the records has been removed.
*/
- (void)removeDataOfTypes:(NSSet<NSString *> *)dataTypes forDataRecords:(NSArray<WKWebsiteDataRecord *> *)dataRecords completionHandler:(void (^)(void))completionHandler;
//清除所有缓存的方法
if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0)
{
NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
}];
} else {
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
NSError *errors;
[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
}
还有一个清除http请求缓存可以了解一下
/*! @abstract Returns the cookie store representing HTTP cookies in this website data store. */
@property (nonatomic, readonly) WKHTTPCookieStore *httpCookieStore API_AVAILABLE(macosx(10.13), ios(11.0));