我试图以编程方式设置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)
}
}
}