我发现了一个有趣的事实,shared会将持久性cookie( )NSHTTPCookieStorage
同步到,谁能告诉我这是一个特性还是一种不可靠的行为?没有找到这方面的文档...isSessionOnly=false
WKWebsiteDataStore.defaultDataStore.httpCookieStore
在 iOS 13.2.2 和 12.1 上测试
POC
步骤1。创建持久性 cookie
var props = Dictionary<HTTPCookiePropertyKey, Any>()
props[HTTPCookiePropertyKey.name] = "HelloWorld"
props[HTTPCookiePropertyKey.value] = "666"
props[HTTPCookiePropertyKey.path] = "/"
props[HTTPCookiePropertyKey.domain] = ".wtf.com"
props[HTTPCookiePropertyKey.expires] = Date(timeIntervalSinceNow: 60 * 60)
let cookie = HTTPCookie(properties: props)
HTTPCookieStorage.shared.setCookie(cookie!)
第2步。从默认的 WK 存储中读取上一个 cookie
WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in
for cookie in cookies {
if cookie.name == "HelloWorld" {
NSLog("boommmmmmm!")
return
}
}
}