码迷,mamicode.com
首页 > Web开发 > 详细

清除WKWebView的缓存

时间:2016-09-09 11:41:58      阅读:1523      评论:0      收藏:0      [点我收藏+]

标签:

清除WKWebView的缓存

OC写法:     swift写法再下下面。

清除WKWebView的缓存,让H5页面一刷新就更新至最新的页面

 

直接上代码~!~~

要区分iOS9.0和8.0两种

- (void)deleteWebCache {

    

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {

        NSSet *websiteDataTypes

        = [NSSet setWithArray:@[

                                WKWebsiteDataTypeDiskCache,

                                //WKWebsiteDataTypeOfflineWebApplicationCache,

                                WKWebsiteDataTypeMemoryCache,

                                //WKWebsiteDataTypeLocalStorage,

                                //WKWebsiteDataTypeCookies,

                                //WKWebsiteDataTypeSessionStorage,

                                //WKWebsiteDataTypeIndexedDBDatabases,

                                //WKWebsiteDataTypeWebSQLDatabases

                                ]];

        //// All kinds of data

        //NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];

        //// Date from

        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];

        //// Execute

        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{

            // Done

        }];

 

    } else {

        

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

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

        NSError *errors;

        [[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];

        

    }

}

 

 

swift:

// MARK: - 清空缓存

    func ClearCache() {

 

        let dateFrom: NSDate = NSDate.init(timeIntervalSince1970: 0)

 

        if #available(iOS 9.0, *) {

            let websiteDataTypes: NSSet = WKWebsiteDataStore.allWebsiteDataTypes()

            WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes as! Set<String>, modifiedSince: dateFrom) {

                print("清空缓存完成")

            }

        } else {

              let libraryPath = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]

            let cookiesFolderPath = libraryPath.stringByAppendingString("/Cookies")

            let errors: NSError

            try? NSFileManager.defaultManager().removeItemAtPath(cookiesFolderPath)

        }

    }

清除WKWebView的缓存

标签:

原文地址:http://www.cnblogs.com/YangFuShun/p/5855829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!