如何从 WKWebView 获取 cookie?
How to get cookies from WKWebView?
如何从 WKWebView 实例中获取所有 cookies?
以下是我迄今为止尝试过的方法:
我尝试使用 - [WKWebView evaluateJavaScript:completionHandler:]
来评估 document.cookie
- 不幸的是结果不包含标记为 HttpOnly.
[=30 的 cookie =]
根据Introducing the Modern WebKit API (WWDC 2014 Session 206), it should be possible to get an response
object from an instance of WKNavigation
. However, according to the class reference,WKNavigation
不包含任何public方法/属性.
由于这个问题在一年后仍未得到解答,我正在 post我的不完美但可行的解决方案:
您可以访问 WKNavigationDelegate
上定义的 - webView:decidePolicyForNavigationResponse:decisionHandler:
方法中的 NSHTTPURLResponse
object。您可以稍后从 HTTP header:
中手动提取 cookie
- (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);
}
如果你有更好的解决方案,请post你的解决方案。
如何从 WKWebView 实例中获取所有 cookies?
以下是我迄今为止尝试过的方法:
我尝试使用
[=30 的 cookie =]- [WKWebView evaluateJavaScript:completionHandler:]
来评估document.cookie
- 不幸的是结果不包含标记为 HttpOnly.根据Introducing the Modern WebKit API (WWDC 2014 Session 206), it should be possible to get an
response
object from an instance ofWKNavigation
. However, according to the class reference,WKNavigation
不包含任何public方法/属性.
由于这个问题在一年后仍未得到解答,我正在 post我的不完美但可行的解决方案:
您可以访问 WKNavigationDelegate
上定义的 - webView:decidePolicyForNavigationResponse:decisionHandler:
方法中的 NSHTTPURLResponse
object。您可以稍后从 HTTP header:
- (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);
}
如果你有更好的解决方案,请post你的解决方案。