Ios 如何在WKWebView httpcookiestore中随请求发送Cookie

Ios 如何在WKWebView httpcookiestore中随请求发送Cookie,ios,cookies,wkwebview,wkwebviewconfiguration,Ios,Cookies,Wkwebview,Wkwebviewconfiguration,在iOS 11应用程序中,我在WKWebView中设置了cookie,并通过在safari inspector中查看cookie来验证它们是否已设置。然而,我不相信他们在我的请求中被传递。除了在webView.configuration.websiteDataStore.httpCookieStore中设置它们之外,我还需要做些什么来让它们发送请求 我将在这里添加我的朴素代码,任何建议都将非常有用: private func setCookies(for request: URLRequest)

在iOS 11应用程序中,我在WKWebView中设置了cookie,并通过在safari inspector中查看cookie来验证它们是否已设置。然而,我不相信他们在我的请求中被传递。除了在
webView.configuration.websiteDataStore.httpCookieStore
中设置它们之外,我还需要做些什么来让它们发送请求

我将在这里添加我的朴素代码,任何建议都将非常有用:

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)
                        }
                    })
                }
            })
        })
    }
}

你解决问题了吗?同样的问题也出现在我的项目中,但这是Objective-C语言中的问题。