10

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

从 iOS 11 及更高版本开始,Apple 提供了一个 API 来执行此操作:WKWebViewsWKWebsiteDataStore有一个 property 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
    }
}

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

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

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

我在这里做错了什么?如何将 cookie 添加到WKWebView(在 iOS 11 及更高版本上)?

4

3 回答 3

14

有点晚了,但我想分享一个对我有用的解决方案,我希望它对在 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)
        }
    }
}

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

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)
                 }
              }
     }
}
于 2018-10-30T03:44:36.677 回答
2

任何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)
于 2018-06-22T00:09:19.930 回答
-1

对于 iOS 11+,你真的不需要担心 cookie 部分,它非常简单。像这样创建你的 cookie。不要让它安全真实

let newcookie = HTTPCookie(properties: [
        .domain: "domain",
        .path: "/",
        .name: "name",
        .value: "vale",
        .secure: "FALSE",
        .expires: NSDate(timeIntervalSinceNow: 31556926)
        ])!

self.webview.configuration.websiteDataStore.httpCookieStore.setCookie(newcookie, completionHandler: {
                        // completion load your url request here. Better to add cookie in Request as well. like this way
       request.addCookies()
//enable cookie through request
        request.httpShouldHandleCookies = true

//load request in your webview.


                    })

在 cookie 值";"之后添加请求扩展

extension URLRequest {

internal mutating func addCookies() {
    var cookiesStr: String = ""

        let mutableRequest = ((self as NSURLRequest).mutableCopy() as? NSMutableURLRequest)!
            cookiesStr += cookie.name + "=" + cookie.value + ";"                

            mutableRequest.setValue(cookiesStr, forHTTPHeaderField: "Cookie")
            self = mutableRequest as URLRequest

     }

}

我还可以看到您没有设置 wkwebview 的 WKWebViewConfiguration。设置 wkwebview 的配置。还要实现WKHTTPCookieStoreObserver和实现函数。

    func cookiesDidChange(in cookieStore: WKHTTPCookieStore) {
    // Must implement otherwise wkwebview cookie not sync properly
    self.httpCookieStore.getAllCookies { (cookies) in
        cookies.forEach({ (cookie) in

        })
    }
}

希望这会奏效。

于 2018-10-30T05:30:02.310 回答