首页 > Web开发 > 详细

清除WKWebView的缓存

时间:2016-09-09 11:39:36      阅读:168      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!