0

A web view with POST data does not POST the body, so I'm missing required parameters of the web view. It works fine in later versions, but does not work with 9.1.

Here is how I'm setting up my webview:

lazy var webView:WKWebView = {
    let view = WKWebView()
    let url:URL = URL(string:<MyURL>)!
    let body:String = String(format: "param1=%@&param2=%@", arguments: [getParam1(), getParam2()])
    var request = URLRequest(url:url)
    request.httpMethod = "POST"
    request.httpBody = body.data(using: .utf8)
    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
    view.navigationDelegate = self
    view.load(request)
    view.backgroundColor = .white
    view.scrollView.delegate = self;
    return view
}()

I've confirmed that my parameters exist when I create body. I get to the page, but it displays an error message about both parameters missing.

Any idea how I can get this working with older versions?

Edit: When I add a breakpoint at the line view.navigationDlegate... and then print the description of the request, I notice requests from later and earlier versions are the same except for some expected bytes which change due to the session token on the device. The range is the exact range I would expect to differ.

Jake T.
  • 4,308
  • 2
  • 20
  • 48

1 Answers1

0

Ahh, this appears to be a bug with the Web Kit that was fixed in iOS 11... pretty major bug, surprised they wouldn't have patched it in earlier versions.

I was really hoping to use the WKWebView instead of UIWebView in order to get better javascript performance, but looks like I'll have to settle.

https://bugs.webkit.org/show_bug.cgi?id=167131

To add on, I resolved this by adding an instance var, var webView:UIView, and in viewDidLoad, I did this:

if #available(iOS 11.0, *) {
    webView = _webView
}
else {
    webView = _webView2
}

Changed my WKWebView lazy var to _webView, added _webView2 lazy var as UIWebView, and implemented the delegates for both. This way, later iOS users get the performance benefits of WKWebView without having to increase the minimum target to 11.0 and losing iOS 9-10 users.

Jake T.
  • 4,308
  • 2
  • 20
  • 48