javascript – Methods to repair standing code 0 in fetch and XMLHttpRequest for response created by WKURLSchemeHandler

[ad_1]

I must embed html5 video games in my app. (Embed into binary, in a such means that i do not want web connection).
I open index.html with loadHTMLString(htmlContentString, baseURL: “myschemename:///index.html”)
I implement WKURLSchemeHandler and face unusual downside:
XMLHttpRequest and fetch request will get standing code “0” and response.okay “false”, even if I returned the info from WKURLSchemeHandler

WKURLSchemeHandler code:

class MyURLSchemeHandler: NSObject, WKURLSchemeHandler {
  func webView(_ webView: WKWebView, begin urlSchemeTask: WKURLSchemeTask) {
    let fileData = ...// code that discover file on disk and browse it
    urlSchemeTask.didReceive(URLResponse(
        url: urlSchemeTask.request.url!,
        mimeType: fileURL.mimeType(),
        expectedContentLength: fileData.depend,
        textEncodingName: "utf-8")
    )
    urlSchemeTask.didReceive(fileData)
    urlSchemeTask.didFinish()
  }
}

Html code that’s

<!DOCTYPE html>
<html>
    <script>
        fetch("myschemename:///knowledge.json")
            .then((response) => {
                if (response.okay) {
                    console.log("okay");
                } else {
                    console.log("error");
                }
            });
    </script>
    <physique>
    </physique>
</html>

[ad_2]

Leave a Reply