Search code examples
javascriptiosswiftasynchronouswkwebview

In iOS 13 and below, how can WKWebView call an async Javascript function (i.e. one that returns a promise)?


iOS 14 has this API, however how can this be achieved in iOS 13 or below?

For example, suppose the JS has a function defined as:

function upperCaseAsync(text) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(text.toUpperCase());
    }, 300);
  });
}

How to call and handle the resulting promise's result? I tried using the synchronous API, but it throws an error:

Error Domain=WKErrorDomain Code=5 "JavaScript execution returned a result of an unsupported type"


Solution

  • You'd need to register callback handler and change your JS function from returning Promise to post webkit message: webkit.messageHandlers['smth'].postMessage('some info if needed')

    let script = WKUserScript(source: <your js code>, injectionTime: .atDocumentStart, forMainFrameOnly: false)
    
    <your web view>.configuration.userContentController.addUserScript(script)
    <your web view>.configuration.userContentController.add(<whatever>, name: "smth")
    
    extension <whatever>: WKScriptMessageHandler {
        
        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
            switch message.name {
                case "smth":
                    break // do smth
                default:
                    return
            }
        }
        
    }