Forum Xamarin Xamarin.iOS

WKWebView EvaluateJavaScript refreshed page

kosteashkosteash Member ✭✭

WKWebView EvaluateJavaScript refreshed page

Best Answer

  • kosteashkosteash Member ✭✭
    Accepted Answer

    Hi ,
    After the last update the problem resolved by itself.

    @LandLu tnx :)

Answers

  • kosteashkosteash Member ✭✭

    How can i that stopped ?

  • LandLuLandLu Member, Xamarin Team Xamurai

    What js code do you run with EvaluateJavaScript?
    Could you please share your code of WKWebView here?

  • kosteashkosteash Member ✭✭
    edited March 25

    @LandLu said:
    What js code do you run with EvaluateJavaScript?
    Could you please share your code of WKWebView here?

    `public class IOSAngularConnector : IAngularConnectorService
    {

        public static IOSAngularConnector Instance = new IOSAngularConnector();
        public WKWebView WebView;
    
        public IOSAngularConnector()
        {
    
        }
    
        public void SendToAngular(string data)
        {
            Instance.WebView.EvaluateJavaScript(string.Format("window.invokeJSFromCSharpAction({0})", data), null);
        }
    
        internal void Initialize(WKWebView webView)
        {
            if (this.WebView == null)
                this.WebView = webView;
        }
    }
    

    public class CustomWKNavigationDelegate:WKNavigationDelegate
    {
    public CustomWKNavigationDelegate()
    {
    }

        public override void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
        {
            //base.DidFinishNavigation(webView, navigation);
    
            IOSAngularConnector.Instance.Initialize(webView);
        }
    
    }
    

    public class CustomWebViewRenderer:ViewRenderer<HybridWebView, WKWebView>, IWKScriptMessageHandler
    {
    private const string INVOKEACTION = "invokeAction";
    private const string JAVASCRIPTFUNCTION = "function invokeCSharpAction(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}";
    private WKUserContentController _UserController;

        protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
        {
            base.OnElementChanged(e);
            if (this.Control == null)
            {
                var config = new WKWebViewConfiguration();
                this._UserController = config.UserContentController;
                var script = new WKUserScript(new NSString(JAVASCRIPTFUNCTION), WKUserScriptInjectionTime.AtDocumentEnd, false);
                this._UserController.AddUserScript(script);
                this._UserController.AddScriptMessageHandler(this, INVOKEACTION);
    
                var wkWebView = new WKWebView(this.Frame, config)
                {
                    NavigationDelegate = new CustomWKNavigationDelegate()
                };
                this.SetNativeControl(wkWebView);
            }
    
            if (e.NewElement != null)
            {
                this.Control.LoadRequest(new NSUrlRequest(new NSUrl(this.Element.Uri)));
            }
    
        }
    

    }

    `

  • LandLuLandLu Member, Xamarin Team Xamurai

    Do you mean this js code:

    Instance.WebView.EvaluateJavaScript(string.Format("window.invokeJSFromCSharpAction({0})", data), null);
    

    What does invokeJSFromCSharpAction look like on your webpage side?
    Try to add a callback to capture some errors:

    Instance.WebView.EvaluateJavaScript(string.Format("window.invokeJSFromCSharpAction('{0}')", data), (result, error) =>
    {
    
    });
    

    Try to add '' here to pass a string parameter.

  • kosteashkosteash Member ✭✭

    @LandLu said:
    Do you mean this js code:

    Instance.WebView.EvaluateJavaScript(string.Format("window.invokeJSFromCSharpAction({0})", data), null);
    

    What does invokeJSFromCSharpAction look like on your webpage side?

    I use Xamarin.Android too and everything works without problems there.

    Try to add a callback to capture some errors:

    Instance.WebView.EvaluateJavaScript(string.Format("window.invokeJSFromCSharpAction('{0}')", data), (result, error) =>
    {
    
    });
    

    Try to add '' here to pass a string parameter.

    No, it did not help

  • LandLuLandLu Member, Xamarin Team Xamurai

    They are two different platforms and you consume the platform features using custom renderer so there could be different effects on iOS.
    What do you mean "did not help"?
    Did your application hit the successful lambda? Try to share a repo here to help me reproduce your issue.

  • kosteashkosteash Member ✭✭
    Accepted Answer

    Hi ,
    After the last update the problem resolved by itself.

    @LandLu tnx :)

Sign In or Register to comment.