如何删除WKWebview cookie [英] How to delete WKWebview cookies

查看:741
本文介绍了如何删除WKWebview cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我这样做

    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
    {
        [storage deleteCookie:cookie];
    }

但它不适用于iOS 8,64位设备。

But it is not working on iOS 8, 64-bit device.

WKWebview的干净饼干还有其他任何方式吗?任何帮助将不胜感激。谢谢。

Any other way the clean cookies of WKWebview? Any help will be appreciated. thanks.

推荐答案

Apple为 iOS 9 发布了新的API,所以现在我们可以使用以下代码删除为 WKWebView 存储的特定于域的Cookie,但这只适用于 iOS 的设备版本 9 稍后

Apple released new APIs for iOS 9, so now we can remove domain specific cookies stored for WKWebView with below code, but this will only work on devices with iOS version 9 or later:

WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
[dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
                 completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) {
                     for (WKWebsiteDataRecord *record  in records)
                     {
                         if ( [record.displayName containsString:@"facebook"])
                         {
                             [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
                                                                       forDataRecords:@[record]
                                                                    completionHandler:^{
                                                                        NSLog(@"Cookies for %@ deleted successfully",record.displayName);
                                                                    }];
                         }
                     }
                 }];

以上代码段肯定适用于 iOS 9 以后。不幸的是,如果我们在 iOS 9 之前对iOS版本使用 WKWebView ,我们仍然需要坚持使用传统方法并删除整个cookie存储如下。

Above snippet will sure work for iOS 9 and later. Unfortunately if we use WKWebView for iOS versions before iOS 9, we still have to stick to traditional method and delete the whole cookies storage as below.

NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
NSError *errors;
[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];

这篇关于如何删除WKWebview cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆