How to set the multiple cookies in WKWebView.I have tried the following approach.
在下面的代码中,我只能保存一个 cookie,即“Abp.TenantId”,另一个没有保存。谁能帮我解决这个问题。我也尝试过使用 NSMutableURLRequest,但它对我也不起作用。
if let url = URL(string: "URL") {
var request = URLRequest(url:url)
request.httpShouldHandleCookies = true
let days: TimeInterval = 30 * 24 * 60 * 60
WKWebsiteDataStore.default().httpCookieStore.add(self)
guard let cookies = HTTPCookie(properties: [
.domain: "domainname/",
.path: "/",
.name: "Abp.AuthToken",
.value: "Abp.AuthToken=token",
.secure: "TRUE",
.expires: NSDate(timeIntervalSinceNow: days),
])else{
return
}
HTTPCookieStorage.shared.setCookie(cookies)
guard let cook = HTTPCookie(properties: [
.domain: "domainname",
.path: "/",
.name: "Abp.TenantId",
.value: "Id",
.secure: "TRUE",
.expires: NSDate(timeIntervalSinceNow: days),
])else{
return
}
HTTPCookieStorage.shared.setCookie(cook)
let arrCookies = HTTPCookieStorage.shared.cookies ?? []
for strcookie in arrCookies {
webView.configuration.websiteDataStore.httpCookieStore.setCookie(strcookie, completionHandler: {
print("cookie setup done")
})
}
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0)
{
self.webView.load(request)
}}