Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
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
4 changes: 4 additions & 0 deletions packages/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.19+6

* Enable opening links that target the "_blank" window (links open in same window).

## 0.3.19+5

* On iOS, always keep contentInsets of the WebView to be 0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
platformThreadHandler = new Handler(context.getMainLooper());
// Allow local storage.
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

methodChannel = new MethodChannel(messenger, "plugins.flutter.io/webview_" + id);
methodChannel.setMethodCallHandler(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void main() {
textDirection: TextDirection.ltr,
child: WebView(
key: _globalKey,
initialUrl: 'https://flutter.dev/',
initialUrl: 'about:blank',
javascriptMode: JavascriptMode.unrestricted,
userAgent: 'Custom_User_Agent1',
onWebViewCreated: (WebViewController controller) {
Expand All @@ -263,7 +263,7 @@ void main() {
textDirection: TextDirection.ltr,
child: WebView(
key: _globalKey,
initialUrl: 'https://flutter.dev/',
initialUrl: 'about:blank',
javascriptMode: JavascriptMode.unrestricted,
userAgent: 'Custom_User_Agent2',
),
Expand Down Expand Up @@ -301,7 +301,7 @@ void main() {
textDirection: TextDirection.ltr,
child: WebView(
key: _globalKey,
initialUrl: 'https://flutter.dev/',
initialUrl: 'about:blank',
javascriptMode: JavascriptMode.unrestricted,
userAgent: 'Custom_User_Agent',
),
Expand All @@ -315,7 +315,7 @@ void main() {
textDirection: TextDirection.ltr,
child: WebView(
key: _globalKey,
initialUrl: 'https://flutter.dev/',
initialUrl: 'about:blank',
javascriptMode: JavascriptMode.unrestricted,
),
),
Expand Down Expand Up @@ -678,6 +678,33 @@ void main() {
final String currentUrl = await controller.currentUrl();
expect(currentUrl, 'https://flutter.dev/');
});

testWidgets('target _blank opens in same window',
(WidgetTester tester) async {
final Completer<WebViewController> controllerCompleter =
Completer<WebViewController>();
final Completer<void> pageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: WebView(
key: GlobalKey(),
onWebViewCreated: (WebViewController controller) {
controllerCompleter.complete(controller);
},
javascriptMode: JavascriptMode.unrestricted,
onPageFinished: (String url) {
pageLoaded.complete(null);
},
),
),
);
final WebViewController controller = await controllerCompleter.future;
await controller.evaluateJavascript('window.open("about:blank", "_blank")');
await pageLoaded.future;
final String currentUrl = await controller.currentUrl();
expect(currentUrl, 'about:blank');
});
}

// JavaScript booleans evaluate to different string values on Android and iOS.
Expand Down
2 changes: 1 addition & 1 deletion packages/webview_flutter/ios/Classes/FlutterWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

NS_ASSUME_NONNULL_BEGIN

@interface FLTWebViewController : NSObject <FlutterPlatformView>
@interface FLTWebViewController : NSObject <FlutterPlatformView, WKUIDelegate>

- (instancetype)initWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
Expand Down
14 changes: 14 additions & 0 deletions packages/webview_flutter/ios/Classes/FlutterWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ - (instancetype)initWithFrame:(CGRect)frame

_webView = [[FLTWKWebView alloc] initWithFrame:frame configuration:configuration];
_navigationDelegate = [[FLTWKNavigationDelegate alloc] initWithChannel:_channel];
_webView.UIDelegate = self;
_webView.navigationDelegate = _navigationDelegate;
__weak __typeof__(self) weakSelf = self;
[_channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
Expand Down Expand Up @@ -398,4 +399,17 @@ - (void)updateUserAgent:(NSString*)userAgent {
}
}

#pragma mark WKUIDelegate

- (WKWebView*)webView:(WKWebView*)webView
createWebViewWithConfiguration:(WKWebViewConfiguration*)configuration
forNavigationAction:(WKNavigationAction*)navigationAction
windowFeatures:(WKWindowFeatures*)windowFeatures {
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}

return nil;
}

@end
2 changes: 1 addition & 1 deletion packages/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
version: 0.3.19+5
version: 0.3.19+6

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we should consider this a breaking change (as it will break any app relying on these links not being opened)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'd consider it breaking. The equivalent code on Android does open these already...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair

homepage: https://github.com/flutter/plugins/tree/master/packages/webview_flutter

environment:
Expand Down