【问题标题】:How to get cookies from WKWebView如何从 WKWebView 获取 cookie
【发布时间】:2017-01-05 09:15:07
【问题描述】:

我有这个加载登录页面的 WKWebView,我需要的是在成功登录(表单提交)后设置的“iPlanetDirectoryPro”cookie(见下图)。所以,我试图存储它以便在另一个 WKWebView 中使用它。有趣的是“sharedHTTPCookieStorage”包含其他 cookie,但不包含“iPlanetDirectoryPro”。

到目前为止我尝试了什么:

  1. 为第一个 WKWebView 和我尝试在其上使用“iPlanetDirectoryPro”的 WKWebView 创建了一个共享进程池并使用了相同的配置。
  2. 我使用这个委托方法 decidePolicyForNavigationResponse 来获取 cookie:

    - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
    NSArray *cookies =[NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:response.URL];
    
    for (NSHTTPCookie *cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
    
    decisionHandler(WKNavigationResponsePolicyAllow);
    }
    
  3. 在 webView 上评估 JavaScript 命令 document.cookie。

有什么想法吗?

【问题讨论】:

    标签: ios cookies wkwebview


    【解决方案1】:

    无论出于何种原因,WKWebView 和 HTTPCookieStorage 都不能完美地协同工作。您需要自己管理 cookie,例如您可以查看 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多