gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:12:07 24 4
gpt4 key购买 nike

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

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

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

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

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

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

最佳答案

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

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

  • 实例化一个 WKWebsiteDataStore 对象
  • 将自定义 cookie 设置为其 httpCookieStore
  • 等待cookie设置
  • 实例化 WKWebView
  • 加载请求

  • 为此,我创建了 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)
    }
    }
    }
    }

    关于ios - WKWebView 无法设置 Cookie (iOS 11+),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50974353/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com