-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[webview_flutter_wkwebview] Add support for javaScriptCanOpenWindowsAutomatically #10608
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
base: main
Are you sure you want to change the base?
[webview_flutter_wkwebview] Add support for javaScriptCanOpenWindowsAutomatically #10608
Conversation
…utomatically Adds support for WKPreferences.javaScriptCanOpenWindowsAutomatically to allow JavaScript's window.open() to work without user interaction on iOS and macOS. Fixes flutter/flutter#112276
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds support for javaScriptCanOpenWindowsAutomatically to allow JavaScript's window.open() to work without user interaction on iOS and macOS. The changes are well-implemented across Dart and Swift files, including the necessary Pigeon interface updates, native implementation, and tests. I have a couple of suggestions to improve the code. One is to refactor the setJavaScriptMode method in webkit_webview_controller.dart to reduce code duplication, which will enhance maintainability. The other is a minor improvement in the new Swift test to ensure better error reporting.
packages/webview_flutter/webview_flutter_wkwebview/darwin/Tests/PreferencesProxyAPITests.swift
Outdated
Show resolved
Hide resolved
…ntation for setJavaScriptCanOpenWindowsAutomatically to accurately reflect that the default value is `false` on iOS and `true` on macOS, matching Apple's official documentation.
LongCatIsLooong
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with nits. Thank you for the contribution!
| /// - iOS: `false` | ||
| /// - macOS: `true` | ||
| /// | ||
| /// See https://developer.apple.com/documentation/webkit/wkpreferences/1536573-javascriptcanopenwindowsautomati |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, the link doesn't seem to be complete (although the website does seem to redirect you to the correct page). Also I doubt this will be rendered as a link in markdown (so people would have to copy and paste this into their browser's URL bar in order to open it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 96e2b0a.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reviewed doc comment again and refined in f630604.
| @@ -1,3 +1,8 @@ | |||
| ## 3.24.0 | |||
|
|
|||
| * Adds support for `javaScriptCanOpenWindowsAutomatically` to allow JavaScript's | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uber nit: maybe
WebKitWebViewControllerCreationParams.javaScriptCanOpenWindowsAutomatically so the reader doesn't have to look for the exact name space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry for the confusion but above commit is outdated.
I fixed it again in 7df6f3e
|
Could you re-format the dart code? See https://github.com/flutter/packages/blob/main/script/tool/README.md#format-code |
|
Thank you for your advice! I applied formatter by the following code: |
bparrishMines
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution! Added a small nit for the implementation.
| await webpagePreferences.setAllowsContentJavaScript(true); | ||
| } | ||
| // Set javaScriptCanOpenWindowsAutomatically on WKPreferences only if explicitly set | ||
| final bool? javaScriptCanOpenWindowsAutomatically = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
You should be able to move this code above the try block as to not have duplicate code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much.
I fixed it in cf4f2c9.
…matically Resolve merge conflicts in webview_flutter_wkwebview CHANGELOG.md and pubspec.yaml, keeping both the 3.24.0 feature entry and the 3.23.6 bugfix entry.
…c in setJavaScriptMode Move the javaScriptCanOpenWindowsAutomatically setting above the try block to eliminate duplication between the iOS 14+ path and the fallback path. The WKPreferences setting is independent of iOS version. Addresses PR flutter#10608 review feedback (bparrishMines)
Remove duplicated behavioral descriptions and Apple URL from the public API doc, keeping pigeon as the single source of truth. This aligns the documentation style with peer parameters in WebKitWebViewControllerCreationParams.
…HANGELOG.md` review comment: flutter#10608 (comment)
This PR adds support for
WKPreferences.javaScriptCanOpenWindowsAutomaticallyto allow JavaScript'swindow.open()to work without user interaction on iOS and macOS.Changes
javaScriptCanOpenWindowsAutomaticallyparameter toWebKitWebViewControllerCreationParamssetJavaScriptCanOpenWindowsAutomaticallymethod via Pigeon APIsetJavaScriptModeis calledUsage
Proof
I checked this behavior with a demo in this PR
The screen capture below shows that 5 seconds after pressing the button, the WebView shows the destination only if javaScriptCanOpenWindowsAutomatically is true.
Screen.Recording.2025-12-16.at.3.40.48.mp4