我有一个可以在普通网页上工作的网址,但是嵌入在WKWebView中我总是让错误会话过期。有没有办法在这个WKWebView上启用cookie?
答案 0 :(得分:0)
您可以保存响应中的Cookie
- (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);
}
并在请求中设置cookie
_request = [[NSMutableURLRequest alloc] initWithURL:url];
[_request setValue:_cookie forHTTPHeaderField:@"Cookie"];
[_webView loadRequest:_request];