美文网首页
wkwebview清除缓存

wkwebview清除缓存

作者: 陌宇凡 | 来源:发表于2018-07-17 14:00 被阅读0次

    在UIWebView下,可以使用

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

    当使用wkwebview时候WKWebView清除cookies的方法(iOS9以上)

    WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];

      [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]

               completionHandler:^(NSArray * __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);

                                        }];

    //               }

                 }

               }];

    iOS9一下用这种方法:

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

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

    NSError *errors;

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

    查看cookie

    NSHTTPCookie *cookie;

    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];

    for (cookie in [cookieJar cookies]) {

      NSLog(@"%@", cookie);

    }

    相关文章

      网友评论

          本文标题:wkwebview清除缓存

          本文链接:https://www.haomeiwen.com/subject/vtgwpftx.html