我想在 XCode 中使用 swift 4.2 将 cookie 添加到 IOS webView 应用程序,但它不起作用。 下面是我尝试应用的代码示例之一,但它不起作用。 谁能帮帮我?
let cookieProps:[HTTPCookiePropertyKey:Any]=[
HTTPCookiePropertyKey.domain:"https://system.fast-apps.com",
HTTPCookiePropertyKey.path:"/",
HTTPCookiePropertyKey.name:"isNative",
HTTPCookiePropertyKey.value:"IwBhE4g"]
if let cookie = HTTPCookie(properties: cookieProps){
HTTPCookieStorage.shared.setCookie(cookie)
}
最佳答案
您可以通过实现以下方法轻松地将 cookie 添加到 cookie 存储中:
var cookies: [HTTPCookie?] = [] {
didSet {
cookies.compactMap { $0 }.forEach {
HTTPCookieStorage.shared.setCookie($0)
}
}
}
func addCookie(for url: URL, key: String, value: AnyObject, path: String = "/", isSecure: Bool = true, expirationTime: TimeInterval) {
cookies.append(HTTPCookie(properties: [
.originURL: url,
.path: path,
.name: key,
.value: value,
.secure: isSecure ? "TRUE" : "FALSE",
.expires: Date(timeIntervalSinceNow: expirationTime)
]))
}
然后,按如下方式使用它:
addCookie(for: url, key: "someKey", value: someValue, expirationTime: 10000)
关于ios - 如何在 Xcode 中向 WebView IOS 应用程序添加 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57089397/