如果有一个主题,人们可以使用新语言Swift轻松了解如何在Webview中管理Cookie?如果您在互联网上办理登机手续,则在您需要实施此计划时,您将无法找到任何有趣的内容。甚至苹果的文档也很差。
有人知道如何在Swift中处理这些过程吗?这是我在Obj-C中找到的:
SEE COOKIES STORED
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];
如果我们能给出一次这样的答案,对每个人都会很好! 干杯!
答案 0 :(得分:40)
试试这段代码:
SEE COOKIES STORED
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)
}
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)
}
答案 1 :(得分:6)
保存Cookie
func saveCookies() {
guard let cookies = HTTPCookieStorage.shared.cookies else {
return
}
let array = cookies.flatMap { (cookie) -> [HTTPCookiePropertyKey: Any]? in
cookie.properties
}
UserDefaults.standard.set(array, forKey: "cookies")
UserDefaults.standard.synchronize()
}
加载Cookie :
func loadCookies() {
guard let cookies = UserDefaults.standard.value(forKey: "cookies") as? [[HTTPCookiePropertyKey: Any]] else {
return
}
cookies.forEach { (cookie) in
guard let cookie = HTTPCookie.init(properties: cookie) else {
return
}
HTTPCookieStorage.shared.setCookie(cookie)
}
}
您可以像下面的代码一样调用这些函数
func webViewDidStartLoad(_ webView: UIWebView) {
loadCookies()
}
func webViewDidFinishLoad(_ webView: UIWebView) {
saveCookies()
}
不要忘记为 webViewDidStartLoad 和 webViewDidFinishLoad
设置WebView代理答案 2 :(得分:3)
感谢swift"翻译" ...只需要将删除更改为!强迫垂头丧气:
var storage : NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in storage.cookies as! [NSHTTPCookie]
{
storage.deleteCookie(cookie)
}
NSUserDefaults.standardUserDefaults()
答案 3 :(得分:2)
以下是如何使用UIWebView的委托功能捕获cookie的完整答案:
sub maximum($) {
($num1) = @_ ;
($num2) = @_ ;
($num2) = @_ ;
$max = $num1;
if ($num2 > $num1) {
$max = $num2;
}
elsif ($num3 > $max ) {
$max = $num3;
}
}
$n1 = $ARGV[0];
chomp($n1);
$n2 = $ARGV[1];
chomp($n2);
$n3 = $ARGV[2];
chomp($n3);
maximum($n1, $n2, $n3);
请记住,Cookie将保存为默认值,此委托函数会调用webview加载完成的所有移动。 (它还使用 Swift 3.0.1 进行了更新)
答案 4 :(得分:2)
如果您正在使用 swift 4 ,则此代码将停用 Cookie ,并删除网址缓存。
let cookieJar : HTTPCookieStorage = HTTPCookieStorage.shared
for cookie in cookieJar.cookies! as [HTTPCookie]{
NSLog("cookie.domain = %@", cookie.domain)
cookieJar.deleteCookie(cookie)
}
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
答案 5 :(得分:0)
swift 3
let storage = HTTPCookieStorage.shared
for cookie in storage.cookies! {
storage.deleteCookie(cookie)
}
答案 6 :(得分:0)
退出api VKontakte,swift 3 +
let dataStore = WKWebsiteDataStore.default()
dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { (records) in
for record in records {
if record.displayName.contains("facebook") {
dataStore.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: [record], completionHandler: {
print("Deleted: " + record.displayName);
})
}
}
}