EvaluateJavascript Android

AlessioCampanelli2AlessioCampanelli2 USMember
edited March 2015 in Xamarin.Android

Hi all,
i'm tring to call the evaluateJavascript in the My WebviewRenderer. this is the code:

public class MyWebViewRenderer : WebViewRenderer
{
    Android.Webkit.WebView mWebView;
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
    {
        base.OnElementChanged(e);
        mWebView = (Android.Webkit.WebView)Control;
        mWebView.SetWebViewClient(new MyWebViewClient());
    }
}

public class MyWebViewClient : Android.Webkit.WebViewClient
{
    public override void OnPageFinished(Android.Webkit.WebView view, string url)
    {
        base.OnPageFinished(view, url);
        view.EvaluateJavascript("document.getElementsByTagName('pre')[0].innerHTML", new JavaScriptResult());
    }
}

class JavaScriptResult : Java.Lang.Object, IValueCallback
{
    public void OnReceiveValue(Java.Lang.Object value)
    {
        string json = (string)value;
    }
}

but i receive this error:

"Java.Lang.NoSuchMethodError: no method with name='evaluateJavascript' signature='(Ljava/lang/String;Landroid/webkit/ValueCallback;)V' in class Landroid/webkit/WebView;"

any suggestions ?

Thanks you very much for help.

Answers

  • hvaughanhvaughan USMember ✭✭✭

    @AlessioCampanelli2

    Ran into the same issue today and found the answer from another post (linked below). The method was not completely setup until Android KitKat (4.4) and so you need to check for that and run the correct method accordingly:

    if(Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat) {
        webView.EvaluateJavascript("javascript:GoBack();", null);
    } else {
        webView.LoadUrl(script);
    }
    

    https://forums.xamarin.com/discussion/comment/78600/#Comment_78600

Sign In or Register to comment.