-
\ No newline at end of file
diff --git a/example/assets/page-3.html b/example/assets/page-3.html
deleted file mode 100644
index 74ac9753..00000000
--- a/example/assets/page-3.html
+++ /dev/null
@@ -1,31 +0,0 @@
-
-
-
-
-
-
-
Document
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/example/lib/inline_example.screen.dart b/example/lib/inline_example.screen.dart
index bb30a943..43eb62f0 100644
--- a/example/lib/inline_example.screen.dart
+++ b/example/lib/inline_example.screen.dart
@@ -6,16 +6,16 @@ class InlineExampleScreen extends StatefulWidget {
_InlineExampleScreenState createState() => new _InlineExampleScreenState();
}
-class User {
- String username;
- String password;
+class Foo {
+ String bar;
+ String baz;
- User({this.username, this.password});
+ Foo({this.bar, this.baz});
Map
toJson() {
return {
- 'username': this.username,
- 'password': this.password
+ 'bar': this.bar,
+ 'baz': this.baz
};
}
}
@@ -54,7 +54,7 @@ class _InlineExampleScreenState extends State {
decoration:
BoxDecoration(border: Border.all(color: Colors.blueAccent)),
child: InAppWebView(
- //initialUrl: "https://mottie.github.io/Keyboard/",
+ //initialUrl: "https://flutter.dev/",
initialFile: "assets/index.html",
initialHeaders: {},
initialOptions: {
@@ -63,8 +63,14 @@ class _InlineExampleScreenState extends State {
},
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
- controller.addJavaScriptHandler('handlerTest', (args) {
- return new User(username: 'user', password: 'secret');
+
+ webView.addJavaScriptHandler('handlerFoo', (args) {
+ return new Foo(bar: 'bar_value', baz: 'baz_value');
+ });
+
+ webView.addJavaScriptHandler('handlerFooWithArgs', (args) {
+ print(args);
+ return [args[0] + 5, !args[1], args[2][0], args[3]['foo']];
});
},
onLoadStart: (InAppWebViewController controller, String url) {
@@ -87,10 +93,21 @@ class _InlineExampleScreenState extends State {
controller.loadUrl(url);
},
onLoadResource: (InAppWebViewController controller, WebResourceResponse response, WebResourceRequest request) {
- print("resource " + request.url);
+ print("Started at: " +
+ response.startTime.toString() +
+ "ms ---> duration: " +
+ response.duration.toString() +
+ "ms " +
+ response.url);
},
onConsoleMessage: (InAppWebViewController controller, ConsoleMessage consoleMessage) {
- print(consoleMessage.message);
+ print("""
+ console output:
+ sourceURL: ${consoleMessage.sourceURL}
+ lineNumber: ${consoleMessage.lineNumber}
+ message: ${consoleMessage.message}
+ messageLevel: ${consoleMessage.messageLevel}
+ """);
},
),
),
diff --git a/example/lib/webview_example.screen.dart b/example/lib/webview_example.screen.dart
index 54c98e7b..3d210bc5 100644
--- a/example/lib/webview_example.screen.dart
+++ b/example/lib/webview_example.screen.dart
@@ -47,8 +47,7 @@ class MyInappBrowser extends InAppBrowser {
}
@override
- void onLoadResource(
- WebResourceResponse response, WebResourceRequest request) {
+ void onLoadResource(WebResourceResponse response, WebResourceRequest request) {
print("Started at: " +
response.startTime.toString() +
"ms ---> duration: " +
@@ -59,7 +58,13 @@ class MyInappBrowser extends InAppBrowser {
@override
void onConsoleMessage(ConsoleMessage consoleMessage) {
- print(consoleMessage.message);
+ print("""
+ console output:
+ sourceURL: ${consoleMessage.sourceURL}
+ lineNumber: ${consoleMessage.lineNumber}
+ message: ${consoleMessage.message}
+ messageLevel: ${consoleMessage.messageLevel}
+ """);
}
}
diff --git a/example/pubspec.yaml b/example/pubspec.yaml
index 8ec2d08e..a92c55d8 100644
--- a/example/pubspec.yaml
+++ b/example/pubspec.yaml
@@ -42,7 +42,6 @@ flutter:
- assets/index.html
- assets/page-1.html
- assets/page-2.html
- - assets/page-3.html
- assets/css/
- assets/images/
diff --git a/lib/flutter_inappbrowser.dart b/lib/flutter_inappbrowser.dart
index 68e015a5..51f0a6b3 100644
--- a/lib/flutter_inappbrowser.dart
+++ b/lib/flutter_inappbrowser.dart
@@ -858,6 +858,7 @@ class InAppWebViewController {
break;
case "onCallJsHandler":
String handlerName = call.arguments["handlerName"];
+ // decode args to json
List args = jsonDecode(call.arguments["args"]);
if (javaScriptHandlersMap.containsKey(handlerName)) {
// convert result to json
@@ -1160,11 +1161,35 @@ class InAppWebViewController {
///The Android implementation uses [addJavascriptInterface](https://developer.android.com/reference/android/webkit/WebView#addJavascriptInterface(java.lang.Object,%20java.lang.String)).
///The iOS implementation uses [addScriptMessageHandler](https://developer.apple.com/documentation/webkit/wkusercontentcontroller/1537172-addscriptmessagehandler?language=objc)
///
- ///The JavaScript function that can be used to call the handler is `window.flutter_inappbrowser.callHandler(handlerName , ...args);`, where `args` are [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).
+ ///The JavaScript function that can be used to call the handler is `window.flutter_inappbrowser.callHandler(handlerName , ...args)`, where `args` are [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).
///The `args` will be stringified automatically using `JSON.stringify(args)` method and then they will be decoded on the Dart side.
///
- ///Also, a [JavaScriptHandlerCallback] can return json data to the JavaScript side.
+ ///In order to call `window.flutter_inappbrowser.callHandler(handlerName , ...args)` properly, you need to wait and listen the JavaScript event `flutterInAppBrowserPlatformReady`.
+ ///This event will be dispatch as soon as the platform (Android or iOS) is ready to handle the `callHandler` method.
+ ///```javascript
+ /// window.addEventListener("flutterInAppBrowserPlatformReady", function(event) {
+ /// console.log("ready");
+ /// });
+ ///```
+ ///
+ ///`window.flutter_inappbrowser.callHandler` returns a JavaScript [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
+ ///that can be used to get the json result returned by [JavaScriptHandlerCallback].
///In this case, simply return data that you want to send and it will be automatically json encoded using [jsonEncode] from the `dart:convert` library.
+ ///
+ ///So, on the JavaScript side, to get data coming from the Dart side, you will use:
+ ///```javascript
+ /// window.addEventListener("flutterInAppBrowserPlatformReady", function(event) {
+ /// window.flutter_inappbrowser.callHandler('handlerFoo').then(function(result) {
+ /// console.log(result, typeof result);
+ /// console.log(JSON.stringify(result));
+ /// });
+ ///
+ /// window.flutter_inappbrowser.callHandler('handlerFooWithArgs', 1, true, ['bar', 5], {foo: 'baz'}).then(function(result) {
+ /// console.log(result, typeof result);
+ /// console.log(JSON.stringify(result));
+ /// });
+ /// });
+ ///```
void addJavaScriptHandler(String handlerName, JavaScriptHandlerCallback callback) {
this.javaScriptHandlersMap[handlerName] = (callback);
}