Skip to content

Commit

Permalink
[webview_flutter_wkwebview] Add a listener for the canGoBack property…
Browse files Browse the repository at this point in the history
… change on the iOS platform. (flutter#8203)

Added a listener callback for the canGoBack property on iOS. Since it can only be effectively implemented on iOS, it is only exposed in the WKWebView layer.

Fixes flutter/flutter#158160
  • Loading branch information
StanleyCocos authored Jan 9, 2025
1 parent c6caf68 commit 6554751
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 3.17.0

* Adds a change listener for the `canGoBack` property. See
`WebKitWebViewController.setOnCanGoBackChange`.
* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.

## 3.16.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ class WebKitWebViewController extends PlatformWebViewController {
},
);

_webView.addObserver(
_webView,
keyPath: 'canGoBack',
options: <NSKeyValueObservingOptions>{
NSKeyValueObservingOptions.newValue,
},
);

final WeakReference<WebKitWebViewController> weakThis =
WeakReference<WebKitWebViewController>(this);
_uiDelegate = _webKitParams.webKitProxy.createUIDelegate(
Expand Down Expand Up @@ -299,6 +307,12 @@ class WebKitWebViewController extends PlatformWebViewController {
final NSUrl? url = change[NSKeyValueChangeKey.newValue] as NSUrl?;
urlChangeCallback(UrlChange(url: await url?.getAbsoluteString()));
}
case 'canGoBack':
if (controller._onCanGoBackChangeCallback != null) {
final bool canGoBack =
change[NSKeyValueChangeKey.newValue]! as bool;
controller._onCanGoBackChangeCallback!(canGoBack);
}
}
};
}),
Expand All @@ -315,6 +329,7 @@ class WebKitWebViewController extends PlatformWebViewController {
bool _zoomEnabled = true;
WebKitNavigationDelegate? _currentNavigationDelegate;

void Function(bool)? _onCanGoBackChangeCallback;
void Function(JavaScriptConsoleMessage)? _onConsoleMessageCallback;
void Function(PlatformWebViewPermissionRequest)? _onPermissionRequestCallback;

Expand Down Expand Up @@ -602,6 +617,12 @@ class WebKitWebViewController extends PlatformWebViewController {
.addUserScript(userScript);
}

/// Sets the listener for canGoBack changes.
Future<void> setOnCanGoBackChange(
void Function(bool) onCanGoBackChangeCallback) async {
_onCanGoBackChangeCallback = onCanGoBackChangeCallback;
}

/// Sets a callback that notifies the host application of any log messages
/// written to the JavaScript console.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter_wkwebview
description: A Flutter plugin that provides a WebView widget based on Apple's WKWebView control.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_wkwebview
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 3.16.3
version: 3.17.0

environment:
sdk: ^3.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,53 @@ window.addEventListener("error", function(e) {
});
});

test('setOnCanGoBackChange', () async {
final MockWKWebViewIOS mockWebView = MockWKWebViewIOS();

late final void Function(
String keyPath,
NSObject object,
Map<NSKeyValueChangeKey, Object?> change,
) webViewObserveValue;

final WebKitWebViewController controller = createControllerWithMocks(
createMockWebView: (
_, {
void Function(
String keyPath,
NSObject object,
Map<NSKeyValueChangeKey, Object?> change,
)? observeValue,
}) {
webViewObserveValue = observeValue!;
return mockWebView;
},
);

verify(
mockWebView.addObserver(
mockWebView,
keyPath: 'canGoBack',
options: <NSKeyValueObservingOptions>{
NSKeyValueObservingOptions.newValue,
},
),
);

late final bool callbackCanGoBack;

await controller.setOnCanGoBackChange(
(bool canGoBack) => callbackCanGoBack = canGoBack);

webViewObserveValue(
'canGoBack',
mockWebView,
<NSKeyValueChangeKey, Object?>{NSKeyValueChangeKey.newValue: true},
);

expect(callbackCanGoBack, true);
});

test('setOnScrollPositionChange', () async {
final WebKitWebViewController controller = createControllerWithMocks();

Expand Down

0 comments on commit 6554751

Please sign in to comment.