我有以下代码来保存cookie,但cookie没有被保存。获取cookie代码显示我的cookie不存在于共享cookie中。 "设置cookie"正在打印但是"得到cookie"没有显示我的cookie。我可以让它保存一个新的cookie但不会覆盖现有的cookie。
func setCookie() {
let infoModel = UserInfoManager.getUserInfoModel()
if infoModel != nil {
let ExpTime = TimeInterval(60 * 60 * 24 * 365)
let cookieProps: [HTTPCookiePropertyKey : Any] = [
HTTPCookiePropertyKey.domain: ".mydomain.com",
HTTPCookiePropertyKey.path: "/",
HTTPCookiePropertyKey.name: "DEV",
HTTPCookiePropertyKey.value: infoModel!.userAccessToken!,
HTTPCookiePropertyKey.secure: "TRUE",
HTTPCookiePropertyKey.expires: NSDate(timeIntervalSinceNow: ExpTime),
HTTPCookiePropertyKey.version: "0"
]
print("WEB REQUEST ACCESS TOKEN \(infoModel!.userAccessToken!)")
if let cookie = HTTPCookie(properties: cookieProps) {
HTTPCookieStorage.shared.setCookie(cookie)
print("\(cookie)")
print("GET COOKIE \(HTTPCookieStorage.shared.cookies)")
}
else {
print("set cookie failed")
}
....
}
答案 0 :(得分:3)
我必须先删除我想要覆盖的cookie。
let cookieJar = HTTPCookieStorage.shared
for cookie in cookieJar.cookies! {
if cookie.name == "DEV" {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}