Skip to content

Commit

Permalink
moved json serialization for JavaScriptBridgeInterface to dart side, fix
Browse files Browse the repository at this point in the history
 #64, fix #46
  • Loading branch information
pichillilorenzo committed Mar 14, 2019
1 parent a480ebf commit 0c49b45
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 360 deletions.
135 changes: 75 additions & 60 deletions .idea/workspace.xml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ dependencies {
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.squareup.okhttp3:mockwebserver:3.11.0'
implementation 'com.google.code.gson:gson:2.8.5'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Expand Down Expand Up @@ -129,6 +130,7 @@ public void onMethodCall(MethodCall call, Result result) {
if (webView != null) {
source = call.argument("source").toString();
webView.injectScriptCode(source, result);
// ((InputMethodManager) this.activity.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
else {
result.success("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.google.gson.Gson;
import com.pichillilorenzo.flutter_inappbrowser.InAppWebView.InAppWebView;

import java.util.HashMap;
Expand Down Expand Up @@ -43,13 +42,12 @@ public void _callHandler(String handlerName, final String _callHandlerID, String

getChannel().invokeMethod("onCallJsHandler", obj, new MethodChannel.Result() {
@Override
public void success(Object o) {
String json = new Gson().toJson(o);
public void success(Object json) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
flutterWebView.webView.evaluateJavascript("window." + name + "[" + _callHandlerID + "](" + json + ");", null);
flutterWebView.webView.evaluateJavascript("window." + name + "[" + _callHandlerID + "](" + json + "); delete window." + name + "[" + _callHandlerID + "];", null);
}
else {
flutterWebView.webView.loadUrl("javascript:window." + name + "[" + _callHandlerID + "](" + json + ");");
flutterWebView.webView.loadUrl("javascript:window." + name + "[" + _callHandlerID + "](" + json + "); delete window." + name + "[" + _callHandlerID + "];");
}
}

Expand Down
1 change: 1 addition & 0 deletions example/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

window.flutter_inappbrowser.callHandler('handlerTest', 1).then(function(result) {
console.log(result, typeof result);
console.log(JSON.stringify(result));
});
});

Expand Down
25 changes: 21 additions & 4 deletions example/lib/inline_example.screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ class InlineExampleScreen extends StatefulWidget {
_InlineExampleScreenState createState() => new _InlineExampleScreenState();
}

class User {
String username;
String password;

User({this.username, this.password});

Map<String, dynamic> toJson() {
return {
'username': this.username,
'password': this.password
};
}
}

class _InlineExampleScreenState extends State<InlineExampleScreen> {
InAppWebViewController webView;
String url = "";
Expand Down Expand Up @@ -40,15 +54,18 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
decoration:
BoxDecoration(border: Border.all(color: Colors.blueAccent)),
child: InAppWebView(
initialUrl: "https://flutter.dev/",
//initialFile: "assets/index.html",
//initialUrl: "https://mottie.github.io/Keyboard/",
initialFile: "assets/index.html",
initialHeaders: {},
initialOptions: {
"useShouldOverrideUrlLoading": true,
"useOnLoadResource": true
//"useShouldOverrideUrlLoading": true,
//"useOnLoadResource": true
},
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
controller.addJavaScriptHandler('handlerTest', (args) {
return new User(username: 'user', password: 'secret');
});
},
onLoadStart: (InAppWebViewController controller, String url) {
print("started $url");
Expand Down
7 changes: 2 additions & 5 deletions ios/Classes/InAppWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -722,12 +722,9 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
else {
var json = "null"
if let r = result {
json = JSONSerializer.toJson(r)
if json == "{}" {
json = "\(r)"
}
json = r as! String
}
self.evaluateJavaScript("window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json));", completionHandler: nil)
self.evaluateJavaScript("window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json)); delete window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)];", completionHandler: nil)
}
})
}
Expand Down
284 changes: 0 additions & 284 deletions ios/Classes/JsonSerializer.swift

This file was deleted.

Loading

0 comments on commit 0c49b45

Please sign in to comment.