shouldInterceptAjaxRequest method

  1. @override
FutureOr<AjaxRequest?>? shouldInterceptAjaxRequest(
  1. AjaxRequest ajaxRequest
)
override

Event fired when an XMLHttpRequest is sent to a server. It gives the host application a chance to take control over the request before sending it. This event is implemented using JavaScript under the hood.

Due to the async nature of this event implementation, it will intercept only async XMLHttpRequests (AjaxRequest.isAsync with true). To be able to intercept sync XMLHttpRequests, use InAppWebViewSettings.interceptOnlyAsyncAjaxRequests to false. If necessary, you should implement your own logic using for example an UserScript overriding the XMLHttpRequest JavaScript object.

ajaxRequest represents the XMLHttpRequest.

Officially Supported Platforms/Implementations:

  • Android WebView:
    • In order to be able to listen this event, check the InAppWebViewSettings.useShouldInterceptAjaxRequest setting documentation. Also, on Android that doesn't support the WebViewFeature.DOCUMENT_START_SCRIPT, unlike iOS that has WKUserScript that can inject javascript code right after the document element is created but before any other content is loaded, in Android the javascript code used to intercept ajax requests is loaded as soon as possible so it won't be instantaneous as iOS. In that case, after the window.addEventListener("flutterInAppWebViewPlatformReady") event is dispatched, the ajax requests can be intercept for sure.
  • iOS WKWebView
  • macOS WKWebView

Parameters - Officially Supported Platforms/Implementations:

  • ajaxRequest: all platforms

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

Implementation

@override
FutureOr<AjaxRequest?>? shouldInterceptAjaxRequest(AjaxRequest ajaxRequest) {
  return null;
}