question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RN postMessage To H5 in iOS not work

See original GitHub issue

Bug 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:

20191012_165756

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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

7reactions
woongbaera89commented, Dec 4, 2019

I had the same problem too. and I fixed it.

If you use “document.addEventListener” in Webview,

// This will be not working in iOS 
document.addEventListener("message", () => { ... your function ... } );

add “window.addEventListener”.

// This works well in both iOS and Android 
document.addEventListener("message", () => { ... your function ... } );
window.addEventListener("message", () => { ... your function ... } );

because, in “react-native-webview/ios/RNCWebView.m” . postMessage function implemented by “window.dispatchEvent” ( look below code )

-(void)postMessage:(NSString *)message
{
  NSDictionary *eventInitDict = @{@"data": message};
  NSString *source = [NSString
    stringWithFormat:@"window.dispatchEvent(new MessageEvent('message', %@));",
    RCTJSONStringify(eventInitDict, NULL)
  ];
  [self injectJavaScript: source];
}
3reactions
haozescommented, Oct 23, 2019

I have the same problem

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found