XMLHttpRequest silently fails when WKWebview not attached to view hierarchy

Originator:elib
Number:rdar://43038986 Date Originated:August 8 2018
Status:Open Resolved:
Product:iOS Product Version:12
Classification: Reproducible:Always
 
Area:
WebKit

Summary:
When loading an HTML page with embedded javascript code, that creates an 'XMLHttpRequest' and uses it to load another resource ,for example a CSV file, the 'onreadystatechange' method of the 'XMLHttpRequest' silently fails on iOS12 when the WKWebview is not attached to the view hierarchy.

I'm including a test project with just the WKWebview, I Also host the relevant HTML page on my server, and also included it and a sample CSV file in the project directory included.

When the project is run, only a few messages will be printed in Xcode terminal, however, if you uncomment line 31 in ViewController.m '[self.view addSubview:self.logicWebView];' you can then see that more logs are being printed, this is because once the WKWebview is added to the view hierarchy 'onreadystatechange' is actually called.

Steps to Reproduce:
1. Create a WKWebview, do not attach it to the view hierarchy.
2. Load a test page that uses an 'XMLHttpRequest' to load a resource.
3. implement 'onreadystatechange' in the test page.
4. try to use 'window.webkit.messageHandlers' to send a message to the client on state change, the message will not be received.


Expected Results:
'onreadystatechange' should work regardless of whether the WKWebview is attached to the view hierarchy or not, as in previous iOS builds.

Actual Results:
'onreadystatechange' is never called on iOS12 when the WKWebview is not attached to the view hierarchy.

Version/Build:
iOS 12 beta 3,4,5.

Comments

Still exists in iOS 12 GM

Only after adding an instance of WKWebView to the view hierarchy (hidden or not) do web requests and JavaScripts that are evaluated with the webview work reliably.

By abraginsky at Sept. 21, 2018, 8:13 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!