• 清除WKWebView的缓存


    清除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)

            }

        }

  • 相关阅读:
    Support for the experimental syntax 'classProperties' isn't currently enabled
    CssSyntaxError (2:1) Unknown word 1 | > 2 | var content = require("!!./index.css");
    vue-transition实现加入购物车效果及其他动画效果实现
    Array.reduce()方法
    Object.keys()返回对象自身可枚举属性组成的数组
    vue + mixin混入对象使用
    el-table + el-form实现可编辑表格字段验证
    深圳面试题
    深圳两年面试题
    JVM中的STW和CMS
  • 原文地址:https://www.cnblogs.com/YangFuShun/p/5855829.html
Copyright © 2020-2023  润新知