Skip to content

Commit

Permalink
fix #74, merge #81 #83, fix javascript ...args parameter of `window…
Browse files Browse the repository at this point in the history
….flutter_inappbrowser.callHandler()`
  • Loading branch information
pichillilorenzo committed Apr 26, 2019
1 parent b2c2f32 commit 36fc04c
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 147 deletions.
240 changes: 125 additions & 115 deletions .idea/workspace.xml

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.1.2

- Fix InAppBrowser crashes the app when i change the page "Lost connection" [#74](https://github.com/pichillilorenzo/flutter_inappbrowser/issues/74)
- Fix javascript `...args` parameter of `window.flutter_inappbrowser.callHandler()`
- Merge Enable setTextZoom function of Android WebViewSetting [#81](https://github.com/pichillilorenzo/flutter_inappbrowser/pull/81)
- Merge bug fix for android build: Android dependency 'androidx.core:core' has different version for the compile (1.0.0) and runtime (1.0.1) classpath [#83](https://github.com/pichillilorenzo/flutter_inappbrowser/pull/83)

## 1.1.1

- Fixed README.md and `addJavaScriptHandler` method documentation
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ All platforms support:
- __domStorageEnabled__: Set to `true` if you want the DOM storage API is enabled. The default value is `false`.
- __useWideViewPort__: Set to `true` if the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is false, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is true and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport will be used. The default value is `true`.
- __safeBrowsingEnabled__: Set to `true` if you want the Safe Browsing is enabled. Safe Browsing allows WebView to protect against malware and phishing attacks by verifying the links. The default value is `true`.
- __textZoom__: Set text scaling of android-webview. The default value is `100`.
- __textZoom__: Set text scaling of the WebView. The default value is `100`.

**iOS** supports these additional options:

Expand Down Expand Up @@ -824,6 +824,7 @@ Opens an `url` in a new `InAppBrowser` instance.
- __useWideViewPort__: Set to `true` if the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is false, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is true and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport will be used. The default value is `true`.
- __safeBrowsingEnabled__: Set to `true` if you want the Safe Browsing is enabled. Safe Browsing allows WebView to protect against malware and phishing attacks by verifying the links. The default value is `true`.
- __progressBar__: Set to `false` to hide the progress bar at the bottom of the toolbar at the top. The default value is `true`.
- __textZoom__: Set text scaling of the WebView. The default value is `100`.

**iOS** supports these additional options:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ else if (options.clearSessionCache)
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(options.useWideViewPort);
settings.setSupportZoom(options.supportZoom);

settings.setTextZoom(options.textZoom);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class InAppWebViewOptions extends Options {
public boolean javaScriptEnabled = true;
public boolean javaScriptCanOpenWindowsAutomatically = false;
public boolean mediaPlaybackRequiresUserGesture = true;
public int textZoom = 100;

public boolean clearSessionCache = false;
public boolean builtInZoomControls = false;
Expand All @@ -22,5 +23,4 @@ public class InAppWebViewOptions extends Options {
public boolean useWideViewPort = true;
public boolean safeBrowsingEnabled = true;

public int textZoom = 100;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class JavaScriptBridgeInterface {
private FlutterWebView flutterWebView;
private InAppBrowserActivity inAppBrowserActivity;

public static final String flutterInAppBroserJSClass = "window." + name + ".callHandler = function(handlerName, ...args) {" +
public static final String flutterInAppBroserJSClass = "window." + name + ".callHandler = function() {" +
"var _callHandlerID = setTimeout(function(){});" +
"window." + name + "._callHandler(handlerName, _callHandlerID, JSON.stringify(args));" +
"window." + name + "._callHandler(arguments[0], _callHandlerID, JSON.stringify(Array.prototype.slice.call(arguments, 1)));" +
"return new Promise(function(resolve, reject) {" +
" window." + name + "[_callHandlerID] = resolve;" +
"});" +
Expand Down
2 changes: 1 addition & 1 deletion example/lib/webview_example.screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class _WebviewExampleScreenState extends State<WebviewExampleScreen> {
return new Center(
child: new RaisedButton(
onPressed: () {
widget.browser.open(url: "https://flutter.dev/", options: {
widget.browser.open(url: "https://google.com", options: {
"useShouldOverrideUrlLoading": true,
"useOnLoadResource": true
});
Expand Down
66 changes: 42 additions & 24 deletions ios/Classes/InAppWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ let JAVASCRIPT_BRIDGE_NAME = "flutter_inappbrowser"

let javaScriptBridgeJS = """
window.\(JAVASCRIPT_BRIDGE_NAME) = {};
window.\(JAVASCRIPT_BRIDGE_NAME).callHandler = function(handlerName, ...args) {
window.\(JAVASCRIPT_BRIDGE_NAME).callHandler = function() {
var _callHandlerID = setTimeout(function(){});
window.webkit.messageHandlers['callHandler'].postMessage( {'handlerName': handlerName, '_callHandlerID': _callHandlerID, 'args': JSON.stringify(args)} );
window.webkit.messageHandlers['callHandler'].postMessage( {'handlerName': arguments[0], '_callHandlerID': _callHandlerID, 'args': JSON.stringify(Array.prototype.slice.call(arguments, 1))} );
return new Promise(function(resolve, reject) {
window.\(JAVASCRIPT_BRIDGE_NAME)[_callHandlerID] = resolve;
});
Expand Down Expand Up @@ -629,31 +629,39 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
if IABController != nil {
arguments["uuid"] = IABController!.uuid
}
getChannel().invokeMethod("onLoadStart", arguments: arguments)
if let channel = getChannel() {
channel.invokeMethod("onLoadStart", arguments: arguments)
}
}

public func onLoadStop(url: String) {
var arguments: [String: Any] = ["url": url]
if IABController != nil {
arguments["uuid"] = IABController!.uuid
}
getChannel().invokeMethod("onLoadStop", arguments: arguments)
if let channel = getChannel() {
channel.invokeMethod("onLoadStop", arguments: arguments)
}
}

public func onLoadError(url: String, error: Error) {
var arguments: [String: Any] = ["url": url, "code": error._code, "message": error.localizedDescription]
if IABController != nil {
arguments["uuid"] = IABController!.uuid
}
getChannel().invokeMethod("onLoadError", arguments: arguments)
if let channel = getChannel() {
channel.invokeMethod("onLoadError", arguments: arguments)
}
}

public func onProgressChanged(progress: Int) {
var arguments: [String: Any] = ["progress": progress]
if IABController != nil {
arguments["uuid"] = IABController!.uuid
}
getChannel().invokeMethod("onProgressChanged", arguments: arguments)
if let channel = getChannel() {
channel.invokeMethod("onProgressChanged", arguments: arguments)
}
}

public func onLoadResource(response: URLResponse, fromRequest request: URLRequest?, withData data: Data, startTime: Int64, duration: Int64) {
Expand Down Expand Up @@ -681,31 +689,39 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
if IABController != nil {
arguments["uuid"] = IABController!.uuid
}
getChannel().invokeMethod("onLoadResource", arguments: arguments)
if let channel = getChannel() {
channel.invokeMethod("onLoadResource", arguments: arguments)
}
}

public func onScrollChanged(x: Int, y: Int) {
var arguments: [String: Any] = ["x": x, "y": y]
if IABController != nil {
arguments["uuid"] = IABController!.uuid
}
getChannel().invokeMethod("onScrollChanged", arguments: arguments)
if let channel = getChannel() {
channel.invokeMethod("onScrollChanged", arguments: arguments)
}
}

public func shouldOverrideUrlLoading(url: URL) {
var arguments: [String: Any] = ["url": url.absoluteString]
if IABController != nil {
arguments["uuid"] = IABController!.uuid
}
getChannel().invokeMethod("shouldOverrideUrlLoading", arguments: arguments)
if let channel = getChannel() {
channel.invokeMethod("shouldOverrideUrlLoading", arguments: arguments)
}
}

public func onConsoleMessage(sourceURL: String, lineNumber: Int, message: String, messageLevel: String) {
var arguments: [String: Any] = ["sourceURL": sourceURL, "lineNumber": lineNumber, "message": message, "messageLevel": messageLevel]
if IABController != nil {
arguments["uuid"] = IABController!.uuid
}
getChannel().invokeMethod("onConsoleMessage", arguments: arguments)
if let channel = getChannel() {
channel.invokeMethod("onConsoleMessage", arguments: arguments)
}
}

public func onCallJsHandler(handlerName: String, _callHandlerID: Int64, args: String) {
Expand All @@ -714,19 +730,21 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
arguments["uuid"] = IABController!.uuid
}

getChannel().invokeMethod("onCallJsHandler", arguments: arguments, result: {(result) -> Void in
if result is FlutterError {
print((result as! FlutterError).message)
}
else if (result as? NSObject) == FlutterMethodNotImplemented {}
else {
var json = "null"
if let r = result {
json = r as! String
if let channel = getChannel() {
channel.invokeMethod("onCallJsHandler", arguments: arguments, result: {(result) -> Void in
if result is FlutterError {
print((result as! FlutterError).message)
}
self.evaluateJavaScript("window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json)); delete window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)];", completionHandler: nil)
}
})
else if (result as? NSObject) == FlutterMethodNotImplemented {}
else {
var json = "null"
if let r = result {
json = r as! String
}
self.evaluateJavaScript("window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json)); delete window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)];", completionHandler: nil)
}
})
}
}

public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
Expand Down Expand Up @@ -794,7 +812,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
}
}

private func getChannel() -> FlutterMethodChannel {
return (IABController != nil) ? SwiftFlutterPlugin.channel! : IAWController!.channel!;
private func getChannel() -> FlutterMethodChannel? {
return (IABController != nil) ? SwiftFlutterPlugin.channel! : ((IAWController != nil) ? IAWController!.channel! : nil);
}
}
4 changes: 3 additions & 1 deletion lib/flutter_inappbrowser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class InAppBrowser {
/// - __useWideViewPort__: Set to `true` if the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is false, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is true and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport will be used. The default value is `true`.
/// - __safeBrowsingEnabled__: Set to `true` if you want the Safe Browsing is enabled. Safe Browsing allows WebView to protect against malware and phishing attacks by verifying the links. The default value is `true`.
/// - __progressBar__: Set to `false` to hide the progress bar at the bottom of the toolbar at the top. The default value is `true`.
/// - __textZoom__: Set text scaling of the WebView. The default value is `100`.
///
/// - **iOS** supports these additional options:
///
Expand Down Expand Up @@ -589,6 +590,7 @@ class InAppWebViewInitialData {
/// - __domStorageEnabled__: Set to `true` if you want the DOM storage API is enabled. The default value is `false`.
/// - __useWideViewPort__: Set to `true` if the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is false, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is true and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport will be used. The default value is `true`.
/// - __safeBrowsingEnabled__: Set to `true` if you want the Safe Browsing is enabled. Safe Browsing allows WebView to protect against malware and phishing attacks by verifying the links. The default value is `true`.
/// - __textZoom__: Set text scaling of the WebView. The default value is `100`.
///
/// **iOS** supports these additional options:
///
Expand Down Expand Up @@ -1214,7 +1216,7 @@ class InAppWebViewController {
}

///Takes a screenshot (in PNG format) of the WebView's visible viewport and returns a `Uint8List`. Returns `null` if it wasn't be able to take it.
///
///__safeBrowsingEnabled__
///**NOTE for iOS**: available from iOS 11.0+.
Future<Uint8List> takeScreenshot() async {
Map<String, dynamic> args = <String, dynamic>{};
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_inappbrowser
description: A Flutter plugin that allows you to add an inline webview or open an in-app browser window (inspired by the popular cordova-plugin-inappbrowser).
version: 1.1.1
version: 1.1.2
author: Lorenzo Pichilli <[email protected]>
homepage: https://github.com/pichillilorenzo/flutter_inappbrowser

Expand Down

0 comments on commit 36fc04c

Please sign in to comment.