3

I have a URL that works on a normal web Page, but embedded in a WKWebView I am always getting the error session expired. Is there some way to enable cookies on this WKWebView?

Md1079
  • 1,360
  • 1
  • 10
  • 28
  • Cookies are enabled by default. Does your website needs javascript, and have you enabled javascript? You can check cookies with Safari's page inspector on mac. – clemens Nov 09 '16 at 18:32
  • https://stackoverflow.com/questions/39772007/wkwebview-persistent-storage-of-cookies/49651579#49651579 – Harish Pathak Apr 04 '18 at 12:54

1 Answers1

0

You can save a cookie from response

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
NSDictionary *headers = [(NSHTTPURLResponse *)navigationResponse.response allHeaderFields];
    if ([headers objectForKey:@"Set-Cookie"] != nil) {
        _cookie = [headers objectForKey:@"Set-Cookie"];
    }
    decisionHandler(WKNavigationResponsePolicyAllow);
}

And set the cookie in request

_request = [[NSMutableURLRequest alloc] initWithURL:url];
[_request setValue:_cookie forHTTPHeaderField:@"Cookie"];
[_webView loadRequest:_request];
Dialogue
  • 311
  • 3
  • 5