WKScriptMessageHandler is not firing RRS feed

  • Question

  • User389147 posted


    I'm curious if there's an issue with this current event (DidReceiveScriptMessage) not firing for anyone else? I've implemented this before and it was successfully working fine, and after a VS for Mac update it no longer worked. The code hasn't changed and it's identical to my Swift version to ensure it's correct.

    All my other events, interfaces, and implementations are working fine except this.


    Wednesday, September 18, 2019 7:26 PM

All replies

  • User369979 posted

    How did you add the AddScriptMessageHandler to the UserContentController? Have you checked you are consuming the same message name? I created a simple webpage for testing it works well on my side. See my attachment for the specific code.

    Thursday, September 19, 2019 5:30 AM
  • User389147 posted

    My code is very similar to yours. A couple differences are the custom message handler inheriting WKScriptMessageHandler and how your message handler is added. It's added directly to the userContentController in the LoadView() call when the wkWebView is instantiated using:

    ``` public override void LoadView() { base.LoadView(); wkWebView = new WKWebView(new CGRect(), HandleWebConfig());

    // other code for programmatic views, auto layout, etc.


    public override void ViewDidLoad() { base.ViewDidLoad();

    wkWebView.UIDelegate = this;
    wkWebView.NavigationDelegate = this;
    // other code and UIViews built for other purposes


    public WKWebViewConfiguration HandleWebConfig() { WKUserContentController userContentController = new WKUserContentController(); userContentController.AddScriptMessageHandler(this, "callbackHandler");

    WKWebViewConfiguration config = new WKWebViewConfiguration();
    config.UserContentController = userContentController;
    return config;

    } ```

    I probably should have been more clear (I'm more used to and inclined to reference Swift), but I inherit the IWKScriptMessageHandler interface and implement it accordingly, and while this did work at one point, it no longer responds to that event in particular.

    As mentioned before, this did work fine, so I'm not sure what all changed to break it? I started to experiment with Previews as well, but considering my test project Swift version works perfectly fine, I didn't think it'd effect the Xamarin version.

    The .html file has a <script> which contains a similar call to webkit, and it is indeed using 'callbackHandler' with matching case as well.

    Note: A lot of this code is stripped out for work purposes, but this shoots the general idea across

    Thursday, September 19, 2019 6:08 AM
  • User369979 posted

    You could make your controller inherit from the IWKScriptMessageHandler we called it weak delegate: https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/delegates-protocols-and-events#strong-delegates-vs-weak-delegates. We can add message handler at the initial time but sometimes this will cause some circular references. So I prefer to add and remove it at the controller's lifecycle event. Can my sample work on your side? If you can't find out the differences you'd better share your sample here. I've no idea about what caused the issue without your sample.

    Thursday, September 19, 2019 6:27 AM
  • User389147 posted

    My current controller does inherit from IWKScriptMessageHandler as mentioned above, and I see your reasoning on the lifecycle events.

    As for your sample, it does catch in the debugger, which isn't what's happening on my end. I'll try and replicate a similar methodology because that's what I'm looking for.

    Thursday, September 19, 2019 6:51 AM
  • User369979 posted

    If you can't share your sample due to the sensitive information you could compare your project with my sample to find out the difference.

    Thursday, September 19, 2019 9:52 AM
  • User389147 posted

    Hi, so I attempted a similar methodology with your sample code by creating a new MessageHandler class.

    It turns out creating a separate class and inheriting WKScriptMessageHandler works fine and will fire the DidReceiveScriptMessage overridden event.

    After seeing this, I tried the IWKScriptMessageHandler interface again and it still won't fire, even adding an unnecessary Export attribute to see if a selector was just off or something.

    Is there something different with that interface that I'm not aware of? I'd like to use it so I can couple it with the partial class that it's going to be used with, otherwise this creates a bit of a headache.

    Friday, September 20, 2019 8:02 PM
  • User369979 posted

    I integrated the individual class to my view controller like:

    public partial class ViewController : UIViewController, IWKScriptMessageHandler

    The event can be triggered successfully. See my attachment.

    Monday, September 23, 2019 9:17 AM
  • User389147 posted

    Checked your sample code, and sure enough it works.

    I'm not sure why my interface isn't working, but I implemented the custom MessageScript handler instead and that works. I'll potentially revisit this in the future, but for now I guess for consistency, the custom handler is the way to go.

    Thanks for your help, and happy .NET Core 3.0 day!

    Monday, September 23, 2019 7:29 PM
  • User369979 posted

    If you felt difficult finding out the issue you could share your samples here. Then I can figure out what really happened on your side.

    Tuesday, September 24, 2019 10:40 AM