简体   繁体   English

如何从WKWebView获取cookie

[英]How to get cookies from WKWebView

I have this WKWebView which loads a login page and what I need is the " iPlanetDirectoryPro " cookie (see picture bellow) that is set after a successful login (form submit). 我有这个WKWebView,它加载一个登录页面,我需要的是在成功登录(表单提交)后设置的“ iPlanetDirectoryPro ” cookie(请参见下面的图片)。 So, I'm trying to store it in order to use it in another WKWebView.. The funny thing is "sharedHTTPCookieStorage" contains the other cookies but not the " iPlanetDirectoryPro " one. 因此,我试图将其存储以便在另一个WKWebView中使用。有趣的是,“ sharedHTTPCookieStorage”包含其他cookie,但不包含“ iPlanetDirectoryPro ”。

在此处输入图片说明

What I tried so far: 到目前为止我尝试过的是:

  1. Created a shared proccess pool and used the same configuration for this first WKWebView and the one I'm trying to use " iPlanetDirectoryPro " on. 创建了一个共享的进程池,并为第一个WKWebView和我尝试在其上使用“ iPlanetDirectoryPro ”的进程使用了​​相同的配置。
  2. I used this delegate method decidePolicyForNavigationResponse to fetch cookies: 我使用此委托方法decisionPolicyForNavigationResponse来获取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. Evaluating JavaScript command document.cookie on webView. 在webView上评估JavaScript命令document.cookie。

Any ideas? 有任何想法吗?

For whatever reason, WKWebView and HTTPCookieStorage aren't working perfectly together. 出于某种原因,WKWebView和HTTPCookieStorage不能完美地协同工作。 You would need to manage the cookies yourself, for example you could look here 您需要自己管理Cookie,例如,您可以在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM