Skip to content

Commit

Permalink
Added api to check if can navigate back and forward (#600)
Browse files Browse the repository at this point in the history
* added callbacks to canGoForward and canGoBack for android

* added flutter tests
added ios implementation

* updated readme
  • Loading branch information
charafau authored Nov 14, 2019
1 parent 38776a8 commit af8273b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,10 @@ Future<Null> goForward();
Future<Null> stopLoading();
```

```dart
Future<bool> canGoBack();
```

```dart
Future<bool> canGoForward();
```
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down
15 changes: 15 additions & 0 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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];
Expand Down
6 changes: 6 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ class FlutterWebviewPlugin {
/// Navigates back on the Webview.
Future<Null> goBack() async => await _channel.invokeMethod('back');

/// Checks if webview can navigate back
Future<bool> canGoBack() async => await _channel.invokeMethod('canGoBack');

/// Checks if webview can navigate back
Future<bool> canGoForward() async => await _channel.invokeMethod('canGoForward');

/// Navigates forward on the Webview.
Future<Null> goForward() async => await _channel.invokeMethod('forward');

Expand Down
9 changes: 8 additions & 1 deletion test/flutter_webview_plugin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ void main() {
webview = new FlutterWebviewPlugin.private(methodChannel);
});


group('Method channel invoke', () {
test('Should invoke close', () async {
webview.close();
Expand All @@ -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);
});
});
}

Expand Down

0 comments on commit af8273b

Please sign in to comment.