Skip to content

Commit afb60fa

Browse files
authored
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
1 parent 80ee964 commit afb60fa

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,10 @@ Future<Null> goForward();
252252
Future<Null> stopLoading();
253253
```
254254

255+
```dart
256+
Future<bool> canGoBack();
257+
```
258+
259+
```dart
260+
Future<bool> canGoForward();
261+
```

android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,19 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
8484
case "cleanCookies":
8585
cleanCookies(call, result);
8686
break;
87+
case "canGoBack":
88+
canGoBack(result);
89+
break;
90+
case "canGoForward":
91+
canGoForward(result);
92+
break;
8793
default:
8894
result.notImplemented();
8995
break;
9096
}
9197
}
9298

93-
void openUrl(MethodCall call, MethodChannel.Result result) {
99+
void openUrl(MethodCall call, MethodChannel.Result result) {
94100
boolean hidden = call.argument("hidden");
95101
String url = call.argument("url");
96102
String userAgent = call.argument("userAgent");
@@ -181,6 +187,19 @@ void close(MethodCall call, MethodChannel.Result result) {
181187
}
182188
}
183189

190+
/**
191+
* Checks if can navigate back
192+
*
193+
* @param result
194+
*/
195+
private void canGoBack(MethodChannel.Result result) {
196+
if (webViewManager != null) {
197+
result.success(webViewManager.canGoBack());
198+
} else {
199+
result.error("Webview is null", null, null);
200+
}
201+
}
202+
184203
/**
185204
* Navigates back on the Webview.
186205
*/
@@ -191,6 +210,18 @@ private void back(MethodCall call, MethodChannel.Result result) {
191210
result.success(null);
192211
}
193212

213+
/**
214+
* Checks if can navigate forward
215+
* @param result
216+
*/
217+
private void canGoForward(MethodChannel.Result result) {
218+
if (webViewManager != null) {
219+
result.success(webViewManager.canGoForward());
220+
} else {
221+
result.error("Webview is null", null, null);
222+
}
223+
}
224+
194225
/**
195226
* Navigates forward on the Webview.
196227
*/

ios/Classes/FlutterWebviewPlugin.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
7373
} else if ([@"reload" isEqualToString:call.method]) {
7474
[self reload];
7575
result(nil);
76+
} else if ([@"canGoBack" isEqualToString:call.method]) {
77+
[self onCanGoBack:call result:result];
78+
} else if ([@"canGoForward" isEqualToString:call.method]) {
79+
[self onCanGoForward:call result:result];
7680
} else {
7781
result(FlutterMethodNotImplemented);
7882
}
@@ -283,6 +287,17 @@ - (void)back {
283287
[self.webview goBack];
284288
}
285289
}
290+
291+
- (void)onCanGoBack:(FlutterMethodCall*)call result:(FlutterResult)result {
292+
BOOL canGoBack = [self.webview canGoBack];
293+
result([NSNumber numberWithBool:canGoBack]);
294+
}
295+
296+
- (void)onCanGoForward:(FlutterMethodCall*)call result:(FlutterResult)result {
297+
BOOL canGoForward = [self.webview canGoForward];
298+
result([NSNumber numberWithBool:canGoForward]);
299+
}
300+
286301
- (void)forward {
287302
if (self.webview != nil) {
288303
[self.webview goForward];

lib/src/base.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ class FlutterWebviewPlugin {
239239
/// Navigates back on the Webview.
240240
Future<Null> goBack() async => await _channel.invokeMethod('back');
241241

242+
/// Checks if webview can navigate back
243+
Future<bool> canGoBack() async => await _channel.invokeMethod('canGoBack');
244+
245+
/// Checks if webview can navigate back
246+
Future<bool> canGoForward() async => await _channel.invokeMethod('canGoForward');
247+
242248
/// Navigates forward on the Webview.
243249
Future<Null> goForward() async => await _channel.invokeMethod('forward');
244250

test/flutter_webview_plugin_test.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ void main() {
1212
webview = new FlutterWebviewPlugin.private(methodChannel);
1313
});
1414

15-
1615
group('Method channel invoke', () {
1716
test('Should invoke close', () async {
1817
webview.close();
@@ -38,6 +37,14 @@ void main() {
3837
webview.show();
3938
verify(methodChannel.invokeMethod('show')).called(1);
4039
});
40+
test('Should invoke canGoBack', () async {
41+
webview.canGoBack();
42+
verify(methodChannel.invokeMethod('canGoBack')).called(1);
43+
});
44+
test('Should invoke canGoForward', () async {
45+
webview.canGoForward();
46+
verify(methodChannel.invokeMethod('canGoForward')).called(1);
47+
});
4148
});
4249
}
4350

0 commit comments

Comments
 (0)