从 WKWebView 获取所有 cookie [英] Getting all cookies from WKWebView

查看:40
本文介绍了从 WKWebView 获取所有 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然使用 NSHTTPCookieStorage.sharedHTTPCookieStorage()UIWebView 获取 cookie 似乎很简单,但 WKWebView 似乎将 cookie 存储在其他地方.>

我做了一些研究,我能够从 NSHTTPURLResponse 对象中获取一些 cookie.然而,这并不包含 WKWebView 使用的所有 cookie:

func webView(webView: WKWebView, DecisionPolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) {如果让 httpResponse = navigationResponse.response 作为?NSHTTPURLResponse {如果让 headers = httpResponse.allHeaderFields 作为?[字符串:字符串],url = httpResponse.URL {让 cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(headers, forURL: url)对于 cookie 中的 cookie {日志调试(cookie.description)logDebug("找到 cookie " + cookie.name + " " + cookie.value)}}}}

奇怪的是,在ios 9中还有一个WKWebsiteDataStore类负责管理WKWebView中的cookies,但是这个类并没有包含获取cookies数据的公共方法:

let storage = WKWebsiteDataStore.defaultDataStore()storage.fetchDataRecordsOfTypes([WKWebsiteDataTypeCookies], completionHandler: { (records) -> Void in记录在记录{logDebug("cookie 记录为" + record.debugDescription)对于 record.dataTypes 中的 dataType {logDebug("数据类型为" + dataType.debugDescription)//获取 cookie 数据??}}})

是否有获取 cookie 数据的解决方法?

解决方案

终于,httpCookieStore for WKWebsiteDataStore 登陆 iOS 11.

https://developer.apple.com/documentation/webkit/wkwebsitedatastore?更改=latest_minor

while getting cookies from UIWebView seems straightforward by using NSHTTPCookieStorage.sharedHTTPCookieStorage(), it seems WKWebView stores the cookies somewhere else.

I did some research, and I was able to get some cookies from the grabbing it from NSHTTPURLResponse object. this, however, does not contain all the cookies used by WKWebView:

func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) {

  if let httpResponse = navigationResponse.response as? NSHTTPURLResponse {
    if let headers = httpResponse.allHeaderFields as? [String: String], url = httpResponse.URL {
      let cookies = NSHTTPCookie.cookiesWithResponseHeaderFields(headers, forURL: url)

      for cookie in cookies {
        logDebug(cookie.description)

        logDebug("found cookie " + cookie.name + " " + cookie.value)
      }
    }
  }
}

Strangely, there's also a class WKWebsiteDataStore in ios 9 that responsible for managing cookies in WKWebView, however, the class does not contain a public method to retrieve the cookies data:

let storage = WKWebsiteDataStore.defaultDataStore()

storage.fetchDataRecordsOfTypes([WKWebsiteDataTypeCookies], completionHandler: { (records) -> Void in
  for record in records {
    logDebug("cookie record is " + record.debugDescription)

    for dataType in record.dataTypes {
      logDebug("data type is " + dataType.debugDescription)

      // get cookie data??
    }
  }
})

Is there a workaround for getting the cookie data?

解决方案

Finally, httpCookieStore for WKWebsiteDataStore landed in iOS 11.

https://developer.apple.com/documentation/webkit/wkwebsitedatastore?changes=latest_minor

这篇关于从 WKWebView 获取所有 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆