HybridWebView EvaluateJS not working for Android Xamarin but works in iOS

Gayathri Subramanian 1 Reputation point
2022-12-15T14:57:18.18+00:00

I am trying to load a web url in my app which is a login page. On entering the details and after authentication it is supposed to return a token . I have used EvaluateJavascript property in HybridWebView . This is working completely fine in iOS , whereas in Android , after the login process it throws 404 error suddenly for the past few days. All the code is same except for the renderers.I do not have a custom control instead have created the ContentViews for hybridwebview as it is prism Template.

Android Renderer.cs

public class HybridWebViewRenderer : WebViewRenderer //ViewRenderer<HybridWebView, Android.Webkit.WebView>  
{  
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)  
    {  
        base.OnElementChanged(e);  

        var webView = e.NewElement as HybridWebView;  
        if (webView != null)  
        {  
            webView.EvaluateJavascript = async (js) =>  
            {  
                var reset = new ManualResetEvent(false);  
                var response = string.Empty;  
                Device.BeginInvokeOnMainThread(() =>  
                {  
                    Control?.EvaluateJavascript(js, new JavascriptCallback((r) => { response = r; reset.Set(); }));  
                });  
                await Task.Run(() => { reset.WaitOne(); });  
                return response;  
            };  
        }  
    }  
}  

internal class JavascriptCallback : Java.Lang.Object, IValueCallback  
{  
    public JavascriptCallback(Action<string> callback)  
    {  
        _callback = callback;  
    }  

    private Action<string> _callback;  
    public void OnReceiveValue(Java.Lang.Object value)  
    {  
        _callback?.Invoke(Convert.ToString(value));  
    }  
}  

In VM : I have declared EvaluateJavascript String innerHtml = await EvaluateJavascript("document.body.innerHTML");

HybridWebView Content View :

public static readonly BindableProperty EvaluateJavascriptProperty =  
            BindableProperty.Create(nameof(EvaluateJavascript),  
                typeof(Func<string, Task<string>>),  
                typeof(HybridWebView),  
                null,  
                BindingMode.OneWayToSource);  

        public Func<string, Task<string>> EvaluateJavascript  
        {  
            get { return (Func<string, Task<string>>)GetValue(EvaluateJavascriptProperty); }  
            set { SetValue(EvaluateJavascriptProperty, value); }  
        }  

calling in the VM :

string store = await EvaluateJavascript("document.body.innerHTML")  

Has anyone come across anything similar and can help me out?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,380 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.