WKWebview ignoring form Posts

I am using WKWebview in my application to load a third party banking provider page. Once the details are entered the suer makes a form submit to post the contents to teh server. But in WKWebview the post information is completely stripped off. Is this a bug in WKWebview or am I missing something in the implementation? The http post body is always nil when I inspect inside the decidePolicyForNavigationAction delegate.




I have seen similar issues reported by others. One suggestion is to create a mutable copy of the request and append your post data inside the WKWebview decide policy delegate. But this is not an option for me as I am dealing with third part web pages?


Is there any solution for this issue?


Thanks.

How you you issuing the POST? From inside the web view? Or by calling

-loadRequest:
from native code?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I'm experiencing this also.

I load a page in WKWebView, then using

[webView evaluateJavaScript:@"document.forms[0].submit();" completionHandler:nil];

I check the request being made in `decidePolicyForNavigationAction` but the `HTTPBody` is empty.

Using the old `UIWebView` and the eexact same method and I get to see the HTTPBody.

Hi,
I'm also getting the same problem when i try to post a data in xamarin iOS using WkWebview

var request = new NSMutableUrlRequest(new NSUrl(new NSString("My Url")));

request.Body = "Posting Data";

request.HttpMethod = "POST";

LoadRequest(request);

It appears this has been fixed in iOS 14.4 (possibly earlier), since the body populates with data for me now.
But I still encounter this problem in iOS 12 & 13.

hey @bgever can you please guide how you have done this. please share your code snippet is possible.

This is for a Cordova app. I'm by no means an Objective-C dev, just read some intro book before, so check for errors. :-)

/**
 * Cordova web view handler calls this method for every page navigation.
 * https://github.com/apache/cordova-ios/blob/master/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m#L538
 */
- (BOOL) shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(WKNavigationType)navType {

    NSString* contentType = [request valueForHTTPHeaderField:@"content-type"];

    if (navType == WKNavigationTypeFormSubmitted && [contentType isEqual:@"application/x-www-form-urlencoded"]) {

        NSData* data = request.HTTPBody;
        NSString* textData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSString* queryString = [NSString stringWithFormat:@"?%@", textData];
        NSURLComponents* urlComponents = [NSURLComponents componentsWithString:queryString];

        for (NSURLQueryItem *item in urlComponents.queryItems) {
            // etc
like this , var params = {"key":"test1"}; will lost 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,target-densitydpi=high-dpi,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>test-for-wkwebview</title>
</head>
<body>
    <div onClick="my_post();">send POST</div>
</body>

<script type="text/javascript">
    function my_post() {
        var params = {"key":"test1"};
        var method = "POST";
        var form = document.createElement("form");
        form.setAttribute("method", method);
        form.setAttribute("target", '_blank');
        form.setAttribute("action", 'http://example.com/');
        for(var key in params){
            if (params.hasOwnProperty(key)) {
                var hiddenFild = document.createElement("input");
                hiddenFild.setAttribute("type", "hidden");
                hiddenFild.setAttribute("name", key);
                hiddenFild.setAttribute("value", params[key]);
            }
            form.appendChild(hiddenFild);
        }
        document.body.appendChild(form);
        form.submit();
    }
    </script>
</html>
WKWebview ignoring form Posts
 
 
Q