Bug 134330 - Why is the WKScriptMessage API only one way?
Summary: Why is the WKScriptMessage API only one way?
Status: RESOLVED DUPLICATE of bug 205239
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit API (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-06-25 20:38 PDT by Ricci Adams
Modified: 2020-02-19 00:18 PST (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ricci Adams 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).
Comment 1 Anders Carlsson 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).
Comment 2 Ricci Adams 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.
Comment 3 Ricci Adams 2017-11-30 00:06:00 PST
Usage (and a better name) might be:

[_webView evaluateJavaScript:@"window.MyBridge" thenInvokeMethod:@"doSomething" withArguments:@[ dictionary ] completionHandler:nil]
Comment 4 Brady Eidson 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 ***