我正在尝试以编程方式设置 cookie,但它不起作用。cookie 给人的印象是它是有效的。然后我把它设置在WKWebsiteDataStore.default().httpCookieStore
. 在异步回调中,我尝试将所有 cookie 从存储中取出。但是,我的 cookie 不存在。去哪儿了!?
let httpCookieStore = WKWebsiteDataStore.default().httpCookieStore
var cookieProperties = [HTTPCookiePropertyKey: Any]()
cookieProperties[.name] = "MarkCookie"
cookieProperties[.value] = "MarkValue"
cookieProperties[.domain] = "localhost"
cookieProperties[.maximumAge] = 1234
cookieProperties[.path] = "/"
let cookie = HTTPCookie(properties: cookieProperties)!
httpCookieStore.setCookie(cookie) {
// This is called fine. The cookie looks ok.
print("Cookie set \(cookie)")
DispatchQueue.main.async {
httpCookieStore.getAllCookies { cookies in
// This is empty!!!!
print(cookies)
}
}
}