Hello,
I'm trying to migrate my code to WKWebView, and I have the following case:
NSAssert(parameters.length > 0); // just to be sure
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = @"POST";
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];
[self.webView loadRequest:request];
It works fine with UIWebView, but for WKWebView it sends a POST request with empty body.
I tried to add a policy decision handler for a WKWebView:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
NSURLRequest *request = navigationAction.request;
// request.HTTPBody is nil here
if ([request.HTTPMethod isEqualToString:@"POST"] && request.HTTPBody == nil && [request isKindOfClass:[NSMutableURLRequest class]])
{
// and even overwriting the body doesn't make it sent to server
[(NSMutableURLRequest*)request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];
}
decisionHandler(WKNavigationActionPolicyAllow);
}
WKWebView is created with a default configuration. Tested in both 9.0 and 8.4 simulators, and 9.0 device.
Any suggestions? Is there some policy that prevents a WKWebView being initialized with a POST request, or is it a bug that survived since iOS 8?
I understand there's hardly a lot of people who need to initialize a web view with POST requests, but still?..
Thanks,
Alex.