无法设置WKWebView设置Cookie(iOS 11+)

nag*_*rrr 2 cookies ios wkwebview wkhttpcookiestore

我拼命尝试将自定义cookie添加到WKWebView实例(不使用Javascript或类似的解决方法)。

从iOS 11及更高版本开始,Apple提供了执行此操作的API:WKWebViews WKWebsiteDataStore具有属性httpCookieStore

这是我的(示例)代码:

import UIKit
import WebKit

class ViewController: UIViewController {

    var webView: WKWebView!

    override func viewDidLoad() {
        webView = WKWebView()
        view.addSubview(webView)
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let cookie = HTTPCookie(properties: [
            HTTPCookiePropertyKey.domain : "google.com",
            HTTPCookiePropertyKey.path : "/",
            HTTPCookiePropertyKey.secure : true,
            HTTPCookiePropertyKey.name : "someCookieKey",
            HTTPCookiePropertyKey.value : "someCookieValue"])!

        let cookieStore = webView.configuration.websiteDataStore.httpCookieStore
        cookieStore.setCookie(cookie) {
            DispatchQueue.main.async {
                self.webView.load(URLRequest(url: URL(string: "https://google.com")!))
            }
        }
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        webView.frame = view.bounds
    }
}
Run Code Online (Sandbox Code Playgroud)

此后,如果我使用,webView.configuration.websiteDataStore.httpCookieStore.getAllCookies(completionHandler:)我会看到我的cookie在cookie列表中。

但是,当使用Safari的开发人员工具(当然使用iOS模拟器)检查Web视图时,cookie不会显示。

我还尝试使用HTTP代理(在本例中为Charles)检查流量,以查看cookie是否包含在我的HTTP请求中。它不是。

我在这里做错了什么?如何向其中添加Cookie WKWebView(在iOS 11及更高版本上)?

小智 5

有点晚了,但是我想分享一个对我有用的解决方案,希望它也可以帮助在iOS 12上也面临相同问题的人。

这是我使用的简化工作流程:

  1. 实例化WKWebsiteDataStore对象
  2. 将自定义Cookie设置为其httpCookieStore
  3. 等待cookie被设置
  4. 实例化WKWebView
  5. 加载请求

为此,我创建了WKWebViewConfiguration的扩展:

extension WKWebViewConfiguration {

static func includeCookie(cookie:HTTPCookie, preferences:WKPreferences, completion: @escaping (WKWebViewConfiguration?) -> Void) {
     let config = WKWebViewConfiguration()
     config.preferences = preferences

     let dataStore = WKWebsiteDataStore.nonPersistent()

     DispatchQueue.main.async {
        let waitGroup = DispatchGroup()

        waitGroup.enter()
        dataStore.httpCookieStore.setCookie(cookie) {
            waitGroup.leave()
        }

        waitGroup.notify(queue: DispatchQueue.main) {
            config.websiteDataStore = dataStore
            completion(config)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于我的示例,我使用它的方式如下:

override func viewDidLoad() {
  self.AddWebView()
}

private func addWebView(){

    let preferences = WKPreferences()
    preferences.javaScriptEnabled = true
    preferences.javaScriptCanOpenWindowsAutomatically = true

    let cookie = HTTPCookie(properties: [
        .domain: COOKIE_DOMAIN,
        .path: "/",
        .name: COOKIE_NAME,
        .value: myCookieValue,
        .secure: "TRUE",
        .expires: NSDate(timeIntervalSinceNow: 3600)
        ])

     //Makes sure the cookie is set before instantiating the webview and initiating the request
     if let myCookie = cookie {
        WKWebViewConfiguration.includeCookie(cookie: myCookie, preferences: preferences, completion: {
           [weak self] config in
              if let `self` = self {
                 if let configuration = config {
                    self.webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width , height: self.view.frame.height), configuration: configuration)

                    self.view.addSubview(self.webView)
                    self.webView.load(self.customRequest)
                 }
              }
     }
}
Run Code Online (Sandbox Code Playgroud)

  • 2020 年 iOS 13 上的表现非常出色,谢谢先生! (2认同)

Ona*_*ato 3

任何请求google.com重定向到www.google.com.

您需要添加www.到 cookie 的域字段。如果域或路径与请求不匹配,则不会发送 cookie。

您可以显式添加 cookie。

let url = URL(string: "https://www.google.com")!
var request = URLRequest(url: url)
if let cookies = HTTPCookieStorage.shared.cookies(for: url) {
    request.allHTTPHeaderFields = HTTPCookie.requestHeaderFields(with: cookies)
}
self.webView.load(request)
Run Code Online (Sandbox Code Playgroud)