<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>134330</bug_id>
          
          <creation_ts>2014-06-25 20:38:12 -0700</creation_ts>
          <short_desc>Why is the WKScriptMessage API only one way?</short_desc>
          <delta_ts>2020-02-19 00:18:46 -0800</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKit API</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>DUPLICATE</resolution>
          <dup_id>205239</dup_id>
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Ricci Adams">webkit.org</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>andersca</cc>
    
    <cc>beidson</cc>
    
    <cc>dieter</cc>
    
    <cc>krzysztof.modras</cc>
    
    <cc>sam</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1018511</commentid>
    <comment_count>0</comment_count>
    <who name="Ricci Adams">webkit.org</who>
    <bug_when>2014-06-25 20:38:12 -0700</bug_when>
    <thetext>The WKScriptMessage/WKScriptMessageHandler API is very useful for sending JSON/plist-esque messages up to the Objective-C from JavaScript.  However, it&apos;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&apos;s great!  However, if I want to send a Plist-esque object down to JavaScript, it&apos;s still a pain to go through.  From an API perspective, it seems weird that you have this nice JS object-&gt;Obj-C object converter, but it&apos;s only one way and you like a nice Obj-C -&gt; 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.&lt;name&gt;.addEventListener(&quot;message&quot;...

(API name are just a suggestion, I&apos;m more concerned about the functionality).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1018725</commentid>
    <comment_count>1</comment_count>
    <who name="Anders Carlsson">andersca</who>
    <bug_when>2014-06-26 13:35:21 -0700</bug_when>
    <thetext>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).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1376240</commentid>
    <comment_count>2</comment_count>
    <who name="Ricci Adams">webkit.org</who>
    <bug_when>2017-11-30 00:03:04 -0800</bug_when>
    <thetext>I&apos;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.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1376241</commentid>
    <comment_count>3</comment_count>
    <who name="Ricci Adams">webkit.org</who>
    <bug_when>2017-11-30 00:06:00 -0800</bug_when>
    <thetext>Usage (and a better name) might be:

[_webView evaluateJavaScript:@&quot;window.MyBridge&quot; thenInvokeMethod:@&quot;doSomething&quot; withArguments:@[ dictionary ] completionHandler:nil]</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1602458</commentid>
    <comment_count>4</comment_count>
    <who name="Brady Eidson">beidson</who>
    <bug_when>2020-01-03 09:07:01 -0800</bug_when>
    <thetext>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 ***</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>