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

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

问题描述

如果有一个主题,人们可以很容易地看到如何使用新语言Swift在网页视图中管理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 STORED

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

删除存储的COOKIES
$ b

DELETE STORED COOKIES

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

如果我们能给出一次答案,这将是很好的每个人!
Cheers!

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中使用UIWebView管理Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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