一尘不染

如何在Swift中使用UIWebView管理cookie

swift

关于一个主题,人们可以使用新语言Swift轻松查看如何在Web视图中管理Cookie呢?如果您在Internet上签到,则在需要实现此功能时不会发现任何有趣的东西。甚至苹果公司的文件也很差。

有人知道如何在Swift中处理这些过程吗?这是我在Obj-C中找到的:

查看存储的Cookie

NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
NSLog(@"%@", cookie);
}

删除存储的Cookie

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

如果我们能给出一次答案的话,对每个人都很好!干杯!


阅读 278

收藏
2020-07-07

共1个答案

一尘不染

试试这个代码:

查看存储的Cookie

    if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies {
        for cookie in cookies {
            NSLog("\(cookie)")
        }
    }

删除存储的Cookie

    var storage : NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
    for cookie in storage.cookies  as! [NSHTTPCookie]{
        storage.deleteCookie(cookie)
    }

迅捷2.0

let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in storage.cookies! {
 storage.deleteCookie(cookie)
}

斯威夫特3.0

if let cookies = HTTPCookieStorage.shared.cookies {
    for cookie in cookies {
        NSLog("\(cookie)")
    }
}

let storage = HTTPCookieStorage.shared
for cookie in storage.cookies! {
    storage.deleteCookie(cookie)
}
2020-07-07