Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added api to check if can navigate back and forward #600

Merged
merged 3 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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