[英]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: 到目前为止我尝试过的是:
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); }
Evaluating JavaScript command document.cookie on webView. 在webView上评估JavaScript命令document.cookie。
Any ideas? 有任何想法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.