Inspired myself to go search for working examples.
Found a near-working example at Stack Overflow at https://stackoverflow.com/questions/65270083/how-does-the-ios-14-api-wkscriptmessagehandlerwithreply-work-for-communicating-w
Pushed a working SwiftUI version to
https://github.com/vaughey/WithReplySample/tree/main
All the trashing I did with data and object types, Sendable, and MainActor was in vain. Undocumented signature for the javascript call was the culprit.
Post
Replies
Boosts
Views
Activity
Never found a single functioning example of WKScriptMessageHandlerWithReply using the replyHandler with the signature func userContentController(
_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage,
replyHandler: @MainActor (Any?, String?) -> Void
Don't believe it has ever worked.
You can reliably add a message handler and capture a javascript message pushed to WkWebView by window.webkit.messageHandlers....postMessage, but the replyHandler back to javascript always fails with JavaScript evaluation error: Error Domain=WKErrorDomain Code=5 "JavaScript execution returned a result of an unsupported type" UserInfo={NSLocalizedDescription=JavaScript execution returned a result of an unsupported type}.
This has been going on for at least 7 years.
Tried every permutation of Sendable, MainActor. Javascipt push reliably works, reply back fails.
Don't think a sample exists.
Would like one.