MARK: - 以下是HTTPCookiePropertyKey中定义的key常量
public static let name: HTTPCookiePropertyKey
public static let value: HTTPCookiePropertyKey
public static let originURL: HTTPCookiePropertyKey
public static let version: HTTPCookiePropertyKey
public static let domain: HTTPCookiePropertyKey
public static let path: HTTPCookiePropertyKey
public static let secure: HTTPCookiePropertyKey
public static let expires: HTTPCookiePropertyKey
public static let comment: HTTPCookiePropertyKey
public static let commentURL: HTTPCookiePropertyKey
public static let discard: HTTPCookiePropertyKey
public static let maximumAge: HTTPCookiePropertyKey
public static let port: HTTPCookiePropertyKey
MARK: - 写入cookie
class func writeCookie() {
// 过期时间1年
let expires: TimeInterval = 60 * 60 * 24 * 365
// 定义一个可变字典存放cookie键值对
var properties: [HTTPCookiePropertyKey : Any] = [:]
properties[.name] = "cookieName"
properties[.path] = "/"
properties[.value] = "cookieValue"
properties[.secure] = false
properties[.domain] = "domain"
properties[.version] = 0
properties[.expires] = Date.init(timeIntervalSinceNow: expires)
// 初始化cookie对象,返回值可选类型
let cookie = HTTPCookie.init(properties: properties)
if let cookie = cookie {
// 保存cookie,地址是沙盒的 /Library/Cookies
HTTPCookieStorage.shared.setCookie(cookie)
}
}
网友评论