6 Replies
      Latest reply: Dec 19, 2016 2:20 PM by eskimo RSS
      pieceofsummer Level 1 Level 1 (0 points)

        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.

        • Re: WKWebView ignores NSURLRequest body
          eskimo Apple Staff Apple Staff (6,015 points)

          It’s likely that this worked with UIWebView as an accident of the implementation.  WKWebView, OTOH, does all of its networking out of process, so such accidents are rare (everything that traverses the inter-process gap has to be explicitly coded to do so).

          Having said that, the documentation doesn’t specifically state that web views must be initialised with a GET, so you’re not doing anything wrong here.

          I recommend you create a small test app and attach it to a bug.  Please post your bug number, just for the record.

          As to a workaround, can you run the POST request yourself (using NSURLSession, say) and then initialise the web view with the response?  Or run the POST request from within the web view using JavaScript?

          Share and Enjoy

          Quinn "The Eskimo!"
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: WKWebView ignores NSURLRequest body
              zhi_xin Level 1 Level 1 (0 points)

              I'm try run the POST request witnin the web uising JavaScript.

              I intercepted the request in NSURLProtocol, but HttpBody is still empty.

              It seems through the WKWebView run the Post request is always empty body.

              Is this a bug or something else?

                • Re: WKWebView ignores NSURLRequest body
                  eskimo Apple Staff Apple Staff (6,015 points)

                  WKWebView runs all of its networking out of process, so your NSURLProtocol shouldn’t see any requests generated by the web view.  NSURLProtocol and WKWebView are pretty much incompatible; if you need to an NSURLProtocol to incept requests made by a web view, you’ll have to stick with UIWebView.

                  My question, however, is why you’re trying to do this?  WKWebView has much better native/JavaScript integration facilities than UIWebView, so many of the older techniques (like NSURLProtocol) are no longer necessary.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: WKWebView ignores NSURLRequest body
                      Bennett.Penn Level 1 Level 1 (0 points)

                      I have the same issue.

                      In fact ,WKWebView/JavascriptCore can't cover all sences.

                      For example, I want upgrape the http to https ,if use the NSURLProtocol to do this ,just regist a URLProtocol change the http scheme to https scheme ,can cover all requset to https. But javascriptCore can't do this.

                       

                      In my demo ,I take a native http request load by WKWebView ,also will ignore the body.  This is not consistent with common sense.

                      I don't understand.

                        • Re: WKWebView ignores NSURLRequest body
                          eskimo Apple Staff Apple Staff (6,015 points)

                          This is not consistent with common sense.

                          Be that as it may, all I can do is explain how WKWebView works.  If you want it to work differently, feel free to file an enhancement request describing your requirements.  However, you’ll still need to find a solution that works for you an current systems.

                          Share and Enjoy

                          Quinn “The Eskimo!”
                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                          let myEmail = "eskimo" + "1" + "@apple.com"

                        • Re: WKWebView ignores NSURLRequest body
                          dmakcpc Level 1 Level 1 (0 points)

                          I also experience this problem. I have filed a bug report, case 29754094.