在iOS 11应用程序中,我已经在我的WKWebView中设置了cookie,并通过在safari检查器中查看它们来验证它们是否已设置好。但是,我不相信他们会在我的请求中被传递。除了在private func setCookies(for request: URLRequest) {
// this seems to contain the session cookies I want
guard let storedCookies = HTTPCookieStorage.shared.cookies(for: request.url!) else { return }
if #available(iOS 11.0, *) {
//what cookies exist currently in the webview store
self.webView.configuration.websiteDataStore.httpCookieStore.getAllCookies({ (webViewCookies) in
storedCookies.forEach({ (cookie) in
if !webViewCookies.contains(cookie) {
//if the webview doesn't contain the stored cookie, add it
self.webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie, completionHandler: {
if let last = storedCookies.last, last == cookie {
//after adding the last cookie, load the request
self.webView.load(request)
}
})
}
})
})
}
}
中设置它们以便让它们在请求中发送之外,我还需要做些什么吗?
我会在这里添加我的天真代码,任何建议都会非常有用:
{{1}}