WKScriptMessageHandler is not firing

marrujoamarrujoa Member ✭✭
edited September 18 in Xamarin.iOS

Hi,

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.

Thanks

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    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.

  • marrujoamarrujoa Member ✭✭
    edited September 19

    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 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

  • LandLuLandLu Member, Xamarin Team Xamurai

    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.

  • marrujoamarrujoa Member ✭✭

    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.

  • LandLuLandLu Member, Xamarin Team Xamurai
    edited September 19

    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.

  • marrujoamarrujoa Member ✭✭
    edited September 20

    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.

  • LandLuLandLu Member, Xamarin Team Xamurai

    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.

  • marrujoamarrujoa Member ✭✭

    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!

  • LandLuLandLu Member, Xamarin Team Xamurai

    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.

Sign In or Register to comment.