Android.Webkit.WebView.AddJavascriptInterface Method
Injects the supplied Java object into this WebView.

Syntax

[Android.Runtime.Register("addJavascriptInterface", "(Ljava/lang/Object;Ljava/lang/String;)V", "GetAddJavascriptInterface_Ljava_lang_Object_Ljava_lang_String_Handler")]
public virtual void AddJavascriptInterface (Java.Lang.Object obj, string interfaceName)

Parameters

object
the Java object to inject into this WebView's JavaScript context. Null values are ignored.
name
the name used to expose the object in JavaScript

Remarks

Injects the supplied Java object into this WebView. The object is injected into the JavaScript context of the main frame, using the supplied name. This allows the Java object's methods to be accessed from JavaScript. For applications targeted to API level NoType:android/os/Build$VERSION_CODES;Href=../../../reference/android/os/Build.VERSION_CODES.html#JELLY_BEAN_MR1 and above, only public methods that are annotated with Android.Webkit.IJavascriptInterface can be accessed from JavaScript. For applications targeted to API level NoType:android/os/Build$VERSION_CODES;Href=../../../reference/android/os/Build.VERSION_CODES.html#JELLY_BEAN or below, all public methods (including the inherited ones) can be accessed, see the important security note below for implications.

Note that injected objects will not appear in JavaScript until the page is next (re)loaded. For example:

java Example

 class JsObject {
    @JavascriptInterface
    public String toString() { return "injectedObject"; }
 }
 webView.addJavascriptInterface(new JsObject(), "injectedObject");
 webView.loadData("", "text/html", null);
 webView.loadUrl("javascript:alert(injectedObject.toString())");

IMPORTANT:

[Android Documentation]

Requirements

Namespace: Android.Webkit
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1