Siy*_*ong 7 cookies macos ios wkwebview
如何从WKWebView实例获取所有 cookie?
以下是我到目前为止所尝试的内容:
我试图- [WKWebView evaluateJavaScript:completionHandler:]用来评估document.cookie- 不幸的是,结果不包含标记为HttpOnly的 cookie .
根据Modern WebKit API简介(WWDC 2014 Session 206),应该可以response从一个实例中获取一个对象WKNavigation.但是,根据类引用,WKNavigation不包含任何公共方法/属性.
由于这个问题一年后还没有得到解答,我发布了我不完美但有效的解决方案:
您可以访问上定义的方法NSHTTPURLResponse中的对象。您稍后可以从 HTTP 标头中手动提取 cookie:- webView:decidePolicyForNavigationResponse:decisionHandler:WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
NSHTTPURLResponse* response = navigationResponse.response;
NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@""]];
for (NSHTTPCookie *cookie in cookies) {
// Do something with the cookie
}
decisionHandler(WKNavigationResponsePolicyAllow);
}
Run Code Online (Sandbox Code Playgroud)
如果您有更好的解决方案,请发布您的解决方案。
| 归档时间: |
|
| 查看次数: |
4181 次 |
| 最近记录: |