In my Code button event not working

Akbarkhan.7088Akbarkhan.7088 USMember
edited October 2016 in Cross Platform with Xamarin

In my PCL Project I want call javascript in iOS native web view and also button event not working ..........................
using System.IO;
using InstaCheckIn;
using InstaCheckIn.iOS;
using Foundation;
using WebKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

//using AppKit;

[assembly: ExportRenderer (typeof(HybridWebView), typeof(HybridWebViewRenderer))]
namespace InstaCheckIn.iOS
{
public class HybridWebViewRenderer : ViewRenderer <HybridWebView,WKWebView >,IWKScriptMessageHandler //ViewRenderer<HybridWebView, WKWebView>, IWKScriptMessageHandler
{
const string JavaScriptFunction = "function invokeCSharpAction(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}";
WKUserContentController userController;

    protected override void OnElementChanged (ElementChangedEventArgs<HybridWebView> e)
    {
        base.OnElementChanged (e);

        if (Control == null) {
            userController = new WKUserContentController ();
            var script = new WKUserScript (new NSString (JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);
            userController.AddUserScript (script);
            userController.AddScriptMessageHandler (this, "invokeAction");

            var config = new WKWebViewConfiguration { UserContentController = userController };
            var webView = new WKWebView (Frame, config);
            SetNativeControl (webView);

        }
        if (e.OldElement != null) {
            userController.RemoveAllUserScripts ();
            userController.RemoveScriptMessageHandler ("invokeAction");
            var hybridWebView = e.OldElement as HybridWebView;
            hybridWebView.Cleanup ();
        }
        if (e.NewElement != null) {
            string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", Element.Uri));
            Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false)));
        }
    }

    public void DidReceiveScriptMessage (WKUserContentController userContentController, WKScriptMessage message)
    {
        Element.InvokeAction (message.Body.ToString ());



    }
}

}

all.zip 41.6M
Tagged:

Answers

  • RyanDohertyRyanDoherty USMember ✭✭
    edited June 2017

    I'm having the same problem. Have you found a solution? I am using the exact code you posted.

    Steps that I took that got me to this problem:

    • I downloaded the example shown in the documentation, updated all packages and it runs great for iOS and Android.
    • I took all the HybridWebView code from that project and put it into my main project
    • Only code modified was the namespace name to match my project's namespace
    • It runs for Android without issue
    • iOS will not fire the DidReceiveScriptMessage method. When I click on the button, nothing happens.

    I don't know how to debug this - all the references look good and all packages have been updated. I get no warning or errors during a build.

    Please help, someone. I have a time-critical project and this is holding me up. Any assistance is greatly appreciated.

Sign In or Register to comment.