WKWebView does not work on evaluateJavaScript() form submit

In our app we are dynamically creating a form with input fields and relying on evaluateJavaScript() function to submit the form and load the url in the decision handler. This approach has been working fine and seems to have been broken in iOS 14 (tested beta 3, 4, 5). Prior to iOS 14 the code below resulted in decidePolicyFor delegate function getting called but it doesn't get called in iOS 14.

The only solution I have found so far is to add the ‘form’ to the dom before calling form.submit() (commented line in javascriptForExecution() function)

Any other suggestions/ideas?

Code Block Swift
override func viewDidLoad() {
super.viewDidLoad()
webView.evaluateJavaScript(javascriptForExecution()) { (result, error) in
if error != nil {
print("Javascript execuption failed: \(error.debugDescription)")
}
}
}

Code Block Swift
private func javascriptForExecution() -> String {
return """
var form = document.createElement('form');
form.method = 'POST';
form.action = 'https://www.foo.com/';
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'Name';
input.value = 'NameValue';
form.appendChild(input);
//document.body.appendChild(form)
form.submit();
"""
}


Code Block Swift
extension UIViewController: WKNavigationDelegate {
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.request.url?.absoluteString == "https://www.foo.com/" {
if let url = URL(string: "https://www.apple.com") {
webView.load(URLRequest(url: url))
}
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
}



Answered by randeep.kakar in 632016022
Response from Apple below

We reviewed your report and determined the behavior you experienced is currently functioning as intended.
This was an intentional change to align with the HTML spec

WKWebView is working properly, and that this change in behavior is due to WebKit aligning more closely with the HTML5 standard, which requires that forms only be submitted if they are ‘connected’ (i.e., part of the DOM).

HTML Standard PR: https://github.com/whatwg/html/pull/2613

The form submit button needs to be associated with a document, and in the document’s node tree.
https://dom.spec.whatwg.org/#connected
It seems WKWebview (which uses webkit) involves in a new technology called "App-Bound Domains" to protect user's privacy. Please refer to App-Bound Domains for more details.

However, I tried the solutions provided in the above document by adding "WKAppBoundDomains" in Info.plist and setting "webViewConfiguration.limitsNavigationsToAppBoundDomains = YES/NO;". They don't work.

For My Instance, I test getting cookies just after setting cookies in WKWebview.configuration.websiteDataStore.httpCookieStore.
It works well on iOS 13/12/11 simulator, but fails on iOS 14 simulator. It return empty cookie array in the callback closure.
Code Block Sample
var wkwebview = WKWebview()
if let cookie = HTTPCookie(properties: [
HTTPCookiePropertyKey.domain: domain,
HTTPCookiePropertyKey.path: "/",
HTTPCookiePropertyKey.name: "Token",
HTTPCookiePropertyKey.secure: "true",
HTTPCookiePropertyKey.value: "\(token)",
HTTPCookiePropertyKey.expires: Date()
]) {
wkwebview.configuration.websiteDataStore.httpCookieStore.setCookie(cookie) {
request.setValue("\(cookie.name)=\(cookie.value);", forHTTPHeaderField: "Cookie")
request.httpShouldHandleCookies = true
wkwebview.load(request)
NSLog("request=\(request.debugDescription)")
wkwebview.configuration.websiteDataStore.httpCookieStore.getAllCookies { (cookies) in
NSLog("----------- print out cookies begin -----------")
for c in cookies {
NSLog("\(c)")
}
NSLog("----------- print out cookies end -----------")
}
}
return
}


I suppose that, Apple doesn't include this feature in XCode 12 beta version.

Can anyone provide more hints?
Accepted Answer
Response from Apple below

We reviewed your report and determined the behavior you experienced is currently functioning as intended.
This was an intentional change to align with the HTML spec

WKWebView is working properly, and that this change in behavior is due to WebKit aligning more closely with the HTML5 standard, which requires that forms only be submitted if they are ‘connected’ (i.e., part of the DOM).

HTML Standard PR: https://github.com/whatwg/html/pull/2613

The form submit button needs to be associated with a document, and in the document’s node tree.
https://dom.spec.whatwg.org/#connected
WKWebView does not work on evaluateJavaScript() form submit
 
 
Q