For Xamarin.iOS/Monotouch it is simple to retrieve a string when evaluating javascript.
e.g.
string elementValue = browser.EvaluateJavascript("document.getElementById('Id').value");
if(elementValue != "")
{
DoSomething();
}
Could anybody provide an example of how to do this for Xamarin. Android/Monodroid Android kitkat and above using Evaluatejavascript()?
Specifically how to use/setup IValueCallback Synchronously.
Android's WebView executes JavaScript asynchronously and passes the result of the execution in a ValueCallback later.
I'm not familiar with Xamarin exactly, but if you can share some of your Android C# code, may be able to help better.
From a Java perspective, where my experience lies:
Essentially, you need to instantiate a ValueCallback object that implements the onReceiveValue
function. When the javascript that you have evaluated finishes executing, the function you declared will be executed, passing in the result of that evaluation as a paramter that is string of JSON. You should then be able to retrieve the result from that JSON.
Hope this helps!
Update: cobbling together some C# that might help. Caveat: I've never written a line of c# before this. But hopefully if it's not quite right will set you on the right path :-)
class JavaScriptResult : public IValueCallback {
public void OnReceiveValue(Java.Lang.Object result) {
Java.Lang.String json = (Java.Lang.String) result;
// |json| is a string of JSON containing the result of your evaluation
}
}
Then you can presumably say something along the lines of
web_view.EvaluateJavaScript("function() { alert('hello world'); return 42; }()",
new JavaScriptResult());
@ksasq's answer is essentially correct but needs some small changes.
First of all, make sure you have Xamarin Android 4.12 installed which adds bindings for KitKat.
Then, since WebView.evaluateJavascript
hasn't been bound in C# flavor, you have to create a class implements IValueCallback
. The tricky part is that class has to be inherited from Java.Lang.Object
.
An example of the class would be like:
private class MyClass : Java.Lang.Object, IValueCallback
{
public void OnReceiveValue(Java.Lang.Object value)
{
//Here goes your implementation.
}
}