onCreateWindow method

  1. @override
Future<bool?>? onCreateWindow(
  1. CreateWindowAction createWindowAction
)

Event fired when the WebView requests the host application to create a new window, for example when trying to open a link with target="_blank" or when window.open() is called by JavaScript side. If the host application chooses to honor this request, it should return true from this method, create a new WebView to host the window. If the host application chooses not to honor the request, it should return false from this method. The default implementation of this method does nothing and hence returns false.

  • createWindowAction represents the request.

NOTE: to allow JavaScript to open windows, you need to set InAppWebViewSettings.javaScriptCanOpenWindowsAutomatically setting to true.

Officially Supported Platforms/Implementations:

  • Android WebView (Official API - WebChromeClient.onCreateWindow):
    • You need to set InAppWebViewSettings.supportMultipleWindows setting to true. Also, if the request has been created using JavaScript (window.open()), then there are some limitation: check the NavigationAction class.
  • iOS WKWebView (Official API - WKUIDelegate.webView):
    • Setting these initial settings InAppWebViewSettings.supportZoom, InAppWebViewSettings.useOnLoadResource, InAppWebViewSettings.useShouldInterceptAjaxRequest, InAppWebViewSettings.useShouldInterceptFetchRequest, InAppWebViewSettings.applicationNameForUserAgent, InAppWebViewSettings.javaScriptCanOpenWindowsAutomatically, InAppWebViewSettings.javaScriptEnabled, InAppWebViewSettings.minimumFontSize, InAppWebViewSettings.preferredContentMode, InAppWebViewSettings.incognito, InAppWebViewSettings.cacheEnabled, InAppWebViewSettings.mediaPlaybackRequiresUserGesture, InAppWebViewSettings.resourceCustomSchemes, InAppWebViewSettings.sharedCookiesEnabled, InAppWebViewSettings.enableViewportScale, InAppWebViewSettings.allowsAirPlayForMediaPlayback, InAppWebViewSettings.allowsPictureInPictureMediaPlayback, InAppWebViewSettings.isFraudulentWebsiteWarningEnabled, InAppWebViewSettings.allowsInlineMediaPlayback, InAppWebViewSettings.suppressesIncrementalRendering, InAppWebViewSettings.selectionGranularity, InAppWebViewSettings.ignoresViewportScaleLimits, InAppWebViewSettings.limitsNavigationsToAppBoundDomains, InAppWebViewSettings.upgradeKnownHostsToHTTPS, will have no effect due to a WKWebView limitation when creating the new window WebView: it's impossible to return the new WKWebView with a different WKWebViewConfiguration instance (see https://developer.apple.com/documentation/webkit/wkuidelegate/1536907-webview). So, these options will be inherited from the caller WebView. Also, note that calling InAppWebViewController.setSettings method using the controller of the new created WebView, it will update also the WebView options of the caller WebView.
  • macOS WKWebView (Official API - WKUIDelegate.webView):
    • Setting these initial settings InAppWebViewSettings.supportZoom, InAppWebViewSettings.useOnLoadResource, InAppWebViewSettings.useShouldInterceptAjaxRequest, InAppWebViewSettings.useShouldInterceptFetchRequest, InAppWebViewSettings.applicationNameForUserAgent, InAppWebViewSettings.javaScriptCanOpenWindowsAutomatically, InAppWebViewSettings.javaScriptEnabled, InAppWebViewSettings.minimumFontSize, InAppWebViewSettings.preferredContentMode, InAppWebViewSettings.incognito, InAppWebViewSettings.cacheEnabled, InAppWebViewSettings.mediaPlaybackRequiresUserGesture, InAppWebViewSettings.resourceCustomSchemes, InAppWebViewSettings.sharedCookiesEnabled, InAppWebViewSettings.enableViewportScale, InAppWebViewSettings.allowsAirPlayForMediaPlayback, InAppWebViewSettings.allowsPictureInPictureMediaPlayback, InAppWebViewSettings.isFraudulentWebsiteWarningEnabled, InAppWebViewSettings.allowsInlineMediaPlayback, InAppWebViewSettings.suppressesIncrementalRendering, InAppWebViewSettings.selectionGranularity, InAppWebViewSettings.ignoresViewportScaleLimits, InAppWebViewSettings.limitsNavigationsToAppBoundDomains, InAppWebViewSettings.upgradeKnownHostsToHTTPS, will have no effect due to a WKWebView limitation when creating the new window WebView: it's impossible to return the new WKWebView with a different WKWebViewConfiguration instance (see https://developer.apple.com/documentation/webkit/wkuidelegate/1536907-webview). So, these options will be inherited from the caller WebView. Also, note that calling InAppWebViewController.setSettings method using the controller of the new created WebView, it will update also the WebView options of the caller WebView.
  • Windows WebView2 (Official API - ICoreWebView2.add_NewWindowRequested)

Parameters - Officially Supported Platforms/Implementations:

  • createWindowAction: all platforms

Use the PlatformInAppBrowserEvents.isMethodSupported method to check if this method is supported at runtime.

Implementation

@override
Future<bool?>? onCreateWindow(CreateWindowAction createWindowAction) async {
  if (createWindowAction.request.url != null) {
    await launchUrl(createWindowAction.request.url!);
    return true;
  }
  return super.onCreateWindow(createWindowAction);
}