postWebMessage method
Post a message to main frame. The embedded application can restrict the messages to a certain target origin. See HTML5 spec for how target origin can be used.
A target origin can be set as a wildcard ("*"). However this is not recommended.
Officially Supported Platforms/Implementations:
- Android WebView (Official API - WebView.postWebMessage):
- This method should only be called if
WebViewFeature.isFeatureSupportedreturnstrueforWebViewFeature.POST_WEB_MESSAGE.
- This method should only be called if
- iOS WKWebView:
- This method is implemented using JavaScript.
- macOS WKWebView:
- This method is implemented using JavaScript.
- Linux WPE WebKit:
- Implemented via JavaScript MessageChannel API.
- Windows WebView2:
- Implemented via JavaScript MessageChannel API.
Parameters - Officially Supported Platforms/Implementations:
message: all platformstargetOrigin: all platforms
Use the PlatformInAppWebViewController.isMethodSupported method to check if this method is supported at runtime.
Implementation
@override
Future<void> postWebMessage({
required WebMessage message,
WebUri? targetOrigin,
}) async {
if (targetOrigin == null) {
targetOrigin = WebUri('');
}
Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('message', () => message.toMap());
args.putIfAbsent('targetOrigin', () => targetOrigin.toString());
await channel?.invokeMethod('postWebMessage', args);
}