如何在 Swift 中使用 UIWebView 管理 cookie [英] How to manage cookies with UIWebView in Swift

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

问题描述

让人们可以轻松了解如何使用新语言 Swift 在 webview 中管理 cookie 的主题怎么样?如果您在互联网上查看,当您需要实施此功能时,您将找不到任何有趣的东西.即使是苹果的文档也很差.

What about have a topic where people can easily see how to manage cookies in a webview using the new language Swift? If you check in internet you won't find anything interesting when you need to implement this. Even the documentation by apple is poor.

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

Do anybody know how to handle these process in Swift? This is what I found but in Obj-C:

查看存储的 COOKIES

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

删除存储的 COOKIES

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

如果我们能一次性回答这个问题,那对每个人都很好!干杯!

It would be nice for everybody if we can give for one time an answer to this! Cheers!

推荐答案

试试这个代码:

查看存储的 COOKIES

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

删除存储的 COOKIES

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

swift 2.0

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

Swift 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)
}

这篇关于如何在 Swift 中使用 UIWebView 管理 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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