From afb60fae88e9d75af1fe3174d6cd55dcd49bab69 Mon Sep 17 00:00:00 2001 From: Rafal Wachol Date: Thu, 14 Nov 2019 18:03:44 +0000 Subject: [PATCH] Added api to check if can navigate back and forward (#600) * added callbacks to canGoForward and canGoBack for android * added flutter tests added ios implementation * updated readme --- README.md | 7 ++++ .../FlutterWebviewPlugin.java | 33 ++++++++++++++++++- ios/Classes/FlutterWebviewPlugin.m | 15 +++++++++ lib/src/base.dart | 6 ++++ test/flutter_webview_plugin_test.dart | 9 ++++- 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e1f2fc96..7bb5abeb 100644 --- a/README.md +++ b/README.md @@ -252,3 +252,10 @@ Future goForward(); Future stopLoading(); ``` +```dart +Future canGoBack(); +``` + +```dart +Future canGoForward(); +``` diff --git a/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java b/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java index 1a775527..cc47009b 100644 --- a/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java +++ b/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java @@ -84,13 +84,19 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) { case "cleanCookies": cleanCookies(call, result); break; + case "canGoBack": + canGoBack(result); + break; + case "canGoForward": + canGoForward(result); + break; default: result.notImplemented(); break; } } - void openUrl(MethodCall call, MethodChannel.Result result) { + void openUrl(MethodCall call, MethodChannel.Result result) { boolean hidden = call.argument("hidden"); String url = call.argument("url"); String userAgent = call.argument("userAgent"); @@ -181,6 +187,19 @@ void close(MethodCall call, MethodChannel.Result result) { } } + /** + * Checks if can navigate back + * + * @param result + */ + private void canGoBack(MethodChannel.Result result) { + if (webViewManager != null) { + result.success(webViewManager.canGoBack()); + } else { + result.error("Webview is null", null, null); + } + } + /** * Navigates back on the Webview. */ @@ -191,6 +210,18 @@ private void back(MethodCall call, MethodChannel.Result result) { result.success(null); } + /** + * Checks if can navigate forward + * @param result + */ + private void canGoForward(MethodChannel.Result result) { + if (webViewManager != null) { + result.success(webViewManager.canGoForward()); + } else { + result.error("Webview is null", null, null); + } + } + /** * Navigates forward on the Webview. */ diff --git a/ios/Classes/FlutterWebviewPlugin.m b/ios/Classes/FlutterWebviewPlugin.m index aee7ff12..3c5aee45 100644 --- a/ios/Classes/FlutterWebviewPlugin.m +++ b/ios/Classes/FlutterWebviewPlugin.m @@ -73,6 +73,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { } else if ([@"reload" isEqualToString:call.method]) { [self reload]; result(nil); + } else if ([@"canGoBack" isEqualToString:call.method]) { + [self onCanGoBack:call result:result]; + } else if ([@"canGoForward" isEqualToString:call.method]) { + [self onCanGoForward:call result:result]; } else { result(FlutterMethodNotImplemented); } @@ -283,6 +287,17 @@ - (void)back { [self.webview goBack]; } } + +- (void)onCanGoBack:(FlutterMethodCall*)call result:(FlutterResult)result { + BOOL canGoBack = [self.webview canGoBack]; + result([NSNumber numberWithBool:canGoBack]); +} + +- (void)onCanGoForward:(FlutterMethodCall*)call result:(FlutterResult)result { + BOOL canGoForward = [self.webview canGoForward]; + result([NSNumber numberWithBool:canGoForward]); +} + - (void)forward { if (self.webview != nil) { [self.webview goForward]; diff --git a/lib/src/base.dart b/lib/src/base.dart index f7f91e78..a7edace6 100644 --- a/lib/src/base.dart +++ b/lib/src/base.dart @@ -239,6 +239,12 @@ class FlutterWebviewPlugin { /// Navigates back on the Webview. Future goBack() async => await _channel.invokeMethod('back'); + /// Checks if webview can navigate back + Future canGoBack() async => await _channel.invokeMethod('canGoBack'); + + /// Checks if webview can navigate back + Future canGoForward() async => await _channel.invokeMethod('canGoForward'); + /// Navigates forward on the Webview. Future goForward() async => await _channel.invokeMethod('forward'); diff --git a/test/flutter_webview_plugin_test.dart b/test/flutter_webview_plugin_test.dart index 052e68b7..0d14947f 100644 --- a/test/flutter_webview_plugin_test.dart +++ b/test/flutter_webview_plugin_test.dart @@ -12,7 +12,6 @@ void main() { webview = new FlutterWebviewPlugin.private(methodChannel); }); - group('Method channel invoke', () { test('Should invoke close', () async { webview.close(); @@ -38,6 +37,14 @@ void main() { webview.show(); verify(methodChannel.invokeMethod('show')).called(1); }); + test('Should invoke canGoBack', () async { + webview.canGoBack(); + verify(methodChannel.invokeMethod('canGoBack')).called(1); + }); + test('Should invoke canGoForward', () async { + webview.canGoForward(); + verify(methodChannel.invokeMethod('canGoForward')).called(1); + }); }); }