RN postMessage To H5 in iOS not work
See original GitHub issueBug description:
I want to test RN postMessage to H5. In Android ,It work all right.But in iOS, it do not work.
To Reproduce:
RN side code : UI_Home.js
import React , {forwardRef}from 'react'
import {View,Button} from 'react-native'
import {WebView} from 'react-native-webview'
export default forwardRef((props,ref) =>{
const {sendMessage,onMessage} = props
return (
<View style={{flex:1}} >
<WebView
ref={ref}
style={{flex:1}}
originWhitelist={['*']}
useWebKit={true}
onMessage={onMessage}
// source={{uri: 'http://192.168.8.178:8080/'}}/>
source={{uri: 'http://192.168.8.178:3000/'}}/>
<Button style={{height:30}} title='RN Send Message to H5' onPress={sendMessage}/>
</View>
)
})
home.js
import React,{Component} from 'react'
import UI_Home from './UI_Home'
export default class Home extends Component{
constructor(props){
super(props)
}
sendMessage = () =>{
this.webView.postMessage(JSON.stringify({test:'hello'}))
}
onMessage = (event) =>{
alert(event.nativeEvent.data)
}
render(){
return (
<UI_Home
ref={(webView)=>{this.webView = webView}}
onMessage = {this.onMessage}
sendMessage={this.sendMessage}/>
)
}
}
Web side code: App.js
import React, {Component} from 'react';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
document.removeEventListener('message', this.onMessage, false)
document.addEventListener('message',this.onMessage,false)
}
onMessage = (event) => {
alert(event.data)
}
sendMessageToRN = () =>{
if(window.ReactNativeWebView){
window.ReactNativeWebView.postMessage('Hello RN ,I`m from H5')
}
}
render() {
return (
<div className="App">
<button onClick={this.sendMessageToRN}>H5 Send Message To RN</button>
</div>
)
}
}
export default App;
When I click RN side Button with postMessage
, Web side addEventListener
in iOS not work.The same fn in Android work all right.
Expected behavior:
Please check and give me some help
Screenshots/Videos:
Environment:
- OS: macOS Mojave
- OS version: 10.14.6
- react-native version: 0.60.5
- react-native-webview version: 7.4.2
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:5
Top Results From Across the Web
RN postMessage To H5 in iOS not work · Issue #947 - GitHub
Bug description: I want to test RN postMessage to H5. In Android ,It work all right.But in iOS, it do not work. To...
Read more >React Native WebView postMessage does not work
Does anyone know why the web app is not receiving the messages? Tested with both Android and iOS. Related documentation: https://facebook.github ...
Read more >Fixing React Native WebView's postMessage for iOS
The rn-webview package works by directing window.postMessage traffic to history.pushState instead. While React Native's iOS implementation cannot handle ...
Read more >React Native WebView 定制与原理 - 河蟹号
React Native 提供WebView 组件调用原生视图组件渲染web 内容,支持iOS/Android 双平台。 在开发RN App 的过程中,使用H5 的场景越来越多。
Read more >ios postmessage - CSDN
响应本机WebView React Native WebView的实现,允许在iOS设备上使用window.postMessage 。安装npm install rn-webview --save或yarn add rn-webview利用无需import ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I had the same problem too. and I fixed it.
If you use “document.addEventListener” in Webview,
add “window.addEventListener”.
because, in “react-native-webview/ios/RNCWebView.m” . postMessage function implemented by “window.dispatchEvent” ( look below code )
I have the same problem