Bug 134330
| Summary: | Why is the WKScriptMessage API only one way? | ||
|---|---|---|---|
| Product: | WebKit | Reporter: | Ricci Adams <webkit.org> |
| Component: | WebKit API | Assignee: | Nobody <webkit-unassigned> |
| Status: | RESOLVED DUPLICATE | ||
| Severity: | Normal | CC: | andersca, beidson, dieter, krzysztof.modras, sam |
| Priority: | P2 | ||
| Version: | 528+ (Nightly build) | ||
| Hardware: | Unspecified | ||
| OS: | Unspecified | ||
Ricci Adams
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 | ||
|---|---|---|
| Add attachment proposed patch, testcase, etc. |
Anders Carlsson
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
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
Usage (and a better name) might be:
[_webView evaluateJavaScript:@"window.MyBridge" thenInvokeMethod:@"doSomething" withArguments:@[ dictionary ] completionHandler:nil]
Brady Eidson
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 ***