Skip to content

[webview_flutter_wkwebview] Add a listener for the canGoBack property change on the iOS platform. #8203

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

Merged
merged 16 commits into from
Jan 9, 2025
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
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
Loading