How to get cookies from WKWebView?

How to get all cookies from an instance of WKWebView?

Here is what I have tried so far:

+7
ios cookies wkwebview macos
source share
1 answer

Since this question has not received an answer in a year, I am sending my imperfect, but working solution:

You can have access to the NSHTTPURLResponse object in the method - webView:decidePolicyForNavigationResponse:decisionHandler: defined in WKNavigationDelegate . You can subsequently extract the cookies manually from the 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); } 

Please post your decision if you have the best.

+3
source share

All Articles