对比跳转URL与页面URL的host,如果为跨域且未做处理,拦截该跳转,重新配置request的cookie信息,再loadRequest一下。(只适合加载mainFrame请求,iframe无效(注意))
-
(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction )navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL currentURL = [NSURL URLWithString:self.pageUrl];if ((![self.crossDomainURL isEqualToString:navigationAction.request.URL.absoluteString]) && (![currentURL.host isEqualToString:navigationAction.request.URL.host])) {
self.crossDomainURL = navigationAction.request.URL.absoluteString; NSMutableURLRequest* request = [navigationAction.request mutableCopy]; NSString* cookieValue = @"session=720bff297ea3866696380368f81fc4e4;token_id=NzIwYmZmMjk3ZWEzODY2Njk2MzgwMzY4ZjgxZmM0ZTQ=;uid=123456;"; [request setValue:cookieValue forHTTPHeaderField:@"Cookie"]; [webView loadRequest:request]; decisionHandler(WKNavigationActionPolicyCancel);
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
、