您的位置:首页 > 移动开发

清除WKWebView cookies

2016-11-11 15:50 363 查看
在UIWebView下,可以使用

[objc] view
plain copy

 





[[NSURLCache sharedURLCache] removeAllCachedResponses];//清除缓存  

来实现清除缓存,但当替换使用WKWebView后,这个方法并不生效了(据说WKWebView不支持,我没找到官方说法~)

不过寻找了一下解决方法,分享一下

--------------IOS9以上----------------

[objc] view
plain copy

 





WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];  

    [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]  

                     completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) {  

                         for (WKWebsiteDataRecord *record  in records)  

                         {  

//                             if ( [record.displayName containsString:@"baidu"]) //取消备注,可以针对某域名清除,否则是全清  

//                             {  

                                 [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes  

                                                                           forDataRecords:@[record]  

                                                                        completionHandler:^{  

                                                                            NSLog(@"Cookies for %@ deleted successfully",record.displayName);  

                                                                        }];  

//                             }  

                         }  

                     }];  

但显然大多小伙伴们都需要支持IOS8的,所以尴尬的是上面的方法暂时用不到,所以还是老老实实的这么干XD

--------------IOS8以上---------------

[objc] view
plain copy

 





NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];  

NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];  

NSError *errors;  

[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors]; 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: