Skip to content
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

[Windows] JS-WebView messaging bridge & multiple WebViews fixes #1617

Conversation

tero-paananen
Copy link
Contributor

@tero-paananen tero-paananen commented Sep 7, 2020

Summary

This PR contains also important changes from my other PR about support of multiple webviews in same time in the app #1603

Adds JS-WebView messaging bridge C++/WinRT component that overwrites old postMessage messaging api window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};

Now messaging works in any kind of source of webview html. Messaging is handled via WebView.AddWebAllowedObject WinRT component "bridge". Same kind of bridge implementation exists in old legacy C# implementation of webview in react-native 0.59. See ReactWebViewManager.cs and line 390

Bridge is enabled after developer defines WebView.onMessage handler.

Bridge is implemented into new Windows Runtime Component (C++/WinRT) component named WebViewBridgeComponent

This PR closes issuess #1600 and #1572

Test Plan

Module and bridge component tested in C# and C++ application.

You can test postMessaging using modules examples app. Message from JS is received in native side by new bridge:
messaging

Setup

ReactNativeWebView and WebViewBridgeComponent have to added into your solution .sln file, for example:

Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WebViewBridgeComponent", "..\node_modules\react-native-webview\windows\WebViewBridgeComponent\WebViewBridgeComponent.vcxproj", "{FCB612AD-FACC-410C-828C-24B5940B2762}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReactNativeWebView", "..\node_modules\react-native-webview\windows\ReactNativeWebView\ReactNativeWebView.vcxproj", "{729D9AF8-CD9E-4427-9F6C-FB757E287729}"
EndProject

Add project reference to ReactNativeWebView into your project file, for example .csproj file:

    <ProjectReference Include="..\..\node_modules\react-native-webview\windows\ReactNativeWebView\ReactNativeWebView.vcxproj">
      <Project>{729d9af8-cd9e-4427-9f6c-fb757e287729}</Project>
      <Name>ReactNativeWebView</Name>
    </ProjectReference>

Compatibility

Windows
React Native Windows 0.62.x

Checklist

  • I have tested this on a device and a simulator

@tero-paananen
Copy link
Contributor Author

tero-paananen commented Sep 8, 2020

This PR closes #1600 and #1572

@Titozzz Titozzz merged commit b930e25 into react-native-webview:master Oct 26, 2020
react-native-community-bot pushed a commit that referenced this pull request Oct 26, 2020
# [10.10.0](v10.9.3...v10.10.0) (2020-10-26)

### Features

* **windows:** JS-WebView messaging bridge & multiple WebViews fixes ([#1617](#1617)) ([b930e25](b930e25))
@react-native-community-bot
Copy link
Collaborator

🎉 This PR is included in version 10.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@pkieltyka
Copy link

nice work. Is it possible to have one document within a WKWebview, which then calls a window.open(url) via javascript, to then open another WKWebview and have cross-window messaging to work via window.parent.postMessage() ?

noproblem23 added a commit to noproblem23/react-native-webview that referenced this pull request Apr 18, 2023
# [10.10.0](react-native-webview/react-native-webview@v10.9.3...v10.10.0) (2020-10-26)

### Features

* **windows:** JS-WebView messaging bridge & multiple WebViews fixes ([#1617](react-native-webview/react-native-webview#1617)) ([b930e25](react-native-webview/react-native-webview@b930e25))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants