RESOLVED DUPLICATE of bug 205239 134330
Why is the WKScriptMessage API only one way?
https://bugs.webkit.org/show_bug.cgi?id=134330
Summary Why is the WKScriptMessage API only one way?
Ricci Adams
Reported 2014-06-25 20:38:12 PDT
The WKScriptMessage/WKScriptMessageHandler API is very useful for sending JSON/plist-esque messages up to the Objective-C from JavaScript. However, it's currently a pain to do the reverse and send messages down to the JavaScript layer (unless I have missed an API call somewhere ;) ) I understand that -[WKWebView evaluateJavaScript:completionHandler:] is being added, and that's great! However, if I want to send a Plist-esque object down to JavaScript, it's still a pain to go through. From an API perspective, it seems weird that you have this nice JS object->Obj-C object converter, but it's only one way and you like a nice Obj-C -> JS object converter. What I would like to see is something like the following: 1) Add -[WKUserContentController postMessageWithBody:name:]. Body would take an (NSNumber, NSString, NSDate, NSArray, NSDictionary, or NSNull). Name would be the name of the message handler 2) On the JavaScript side, this would dispatch a ScriptMessageEvent event that users could listen to via window.webkit.messageHandlers.<name>.addEventListener("message"... (API name are just a suggestion, I'm more concerned about the functionality).
Attachments
Anders Carlsson
Comment 1 2014-06-26 13:35:21 PDT
Sounds like a good suggestion. Probably need a way to post a message to a single WKWebView, as well as broadcast messages (WKUserContentControllers can be shared between multiple WKWebViews).
Ricci Adams
Comment 2 2017-11-30 00:03:04 PST
I'm hitting this again when trying to adopt WKWebView. Specifically, I need the ability to easily send an NSDictionary as a object. Even some kind of -[WKWebView evaluateJavaScript:andThenCallTheResultOfThatWithSomeArguments:completionHandler:] API would work.
Ricci Adams
Comment 3 2017-11-30 00:06:00 PST
Usage (and a better name) might be: [_webView evaluateJavaScript:@"window.MyBridge" thenInvokeMethod:@"doSomething" withArguments:@[ dictionary ] completionHandler:nil]
Brady Eidson
Comment 4 2020-01-03 09:07:01 PST
The functionality requested is now encompassed by https://bugs.webkit.org/show_bug.cgi?id=205239 in the WebKit project As far as when it may or may not be API, that’s a vendor (e.g. Apple) decision that wont be announced in the WebKit project *** This bug has been marked as a duplicate of bug 205239 ***
Note You need to log in before you can comment on or make changes to this bug.