React Native的WebView与html双向通信

安卓、ios原生与html双向通信相对简单且成熟,但是React Native与html的双向通信存在那么一丢丢的坑

一、参数配置

导包

import { WebView } from 'react-native-webview'

使用

<WebView
    ref={ref => this.webViewRef = ref}
    source={{ uri: url }}
    // source={{ html }}
    javaScriptEnabled
    useWebKit
    allowFileAccess
    startInLoadingState
    onLoadStart={this.onLoadStart}
    onError={this.onError}
    onLoad={this.onLoad}
    onLoadEnd={this.onLoadEnd}
    renderLoading={this.renderLoading}
    renderError={this.renderError}
    javaScriptCanOpenWindowsAutomatically
    onMessage={this.handleMessage}
    injectedJavaScript={`(function() {window.postMessage = function(data) {window.ReactNativeWebView.postMessage(data)}})();`}

相关回调函数

onLoadStart = ({ nativeEvent }) => {
    console.log('onLoadStart', nativeEvent)
}
onError = ({ nativeEvent }) => {
    console.log('onError', nativeEvent)
}
onLoad = ({ nativeEvent }) => {
    this.setState({ title: nativeEvent.title })
    console.log('onLoad', nativeEvent)
}
onLoadEnd = ({ nativeEvent }) => {
    console.log('onLoadEnd', nativeEvent)
}
renderLoading = () => {
    console.log('renderLoading')
}
renderError = ({ nativeEvent }) => {
    console.log('renderError', nativeEvent)
}
handleMessage = async ({ nativeEvent }) => {
    const { actionType, extra } = nativeEvent.data && JSON.parse(nativeEvent.data) || {}
}

二、坑点

2.1、react-native-webview使用postMessage后H5不能监听问题

注意:这里安卓用document,ios用window,否则会出现react-native-webview使用postMessage后H5不能监听问题

/* 监听rn消息 */
const eventListener = nativeEvent => {
    //解析数据actionType、extra
    const {actionType, extra} = nativeEvent.data && JSON.parse(nativeEvent.data) || {}
}
//安卓用document,ios用window
window.addEventListener('message', eventListener);
document.addEventListener('message', eventListener);

2.2、RN的webview的onMessage监听不到的问题

injectedJavaScript={`(function() {window.postMessage = function(data) {window.ReactNativeWebView.postMessage(data)}})();`}

注意这里要加上injectedJavaScript,它重写window.postMessage方法解决了RN的webview的onMessage监听不到的问题

三、React Native与html双向通信

3.1、RN发数据到WebView

RN通过postMessage发送数据

const temp = {
    actionType: 'takePhoto',
    extra: {
        fileId: '1522501682737836034',
        fileUrl: 'https://gongbao-phoenix.oss-cn-hangzhou.aliyuncs.com/test/330100/1/2022/05/06/2efb943b439146ed86db0ad920c7edaf.jpg',
        originalFileName: 'E91FDC71-FD9C-49B9-B038-529C9CDC149B.jpg',
        tag: 'not_use',
        unionId: 'f143153ed07a428fa6308d6f73b1a2b9',
    },
}
this.webViewRef.postMessage(JSON.stringify(temp))

webView接受数据

/* 监听rn消息 */
const eventListener = nativeEvent => {
    const {actionType, extra} = nativeEvent.data && JSON.parse(nativeEvent.data) || {}
    
}
//安卓用document,ios用window 
window.addEventListener('message', eventListener);
document.addEventListener('message', eventListener);

3.2、WebView发数据到RN

WebView通过postMessage发送数据

const action = {
    actionType: 'viewPicture',
    extra: {
        pics: ['https://tva1.sinaimg.cn/large/e6c9d24ely1h0svk2jlcej20ps11ewld.jpg'],
        defaultIndex: 0,
    },
}
window.postMessage(JSON.stringify(action));

RN注册onMessage接收

onMessage={this.handleMessage}
handleMessage = async ({ nativeEvent }) => {
    const { actionType, extra } = nativeEvent.data && JSON.parse(nativeEvent.data) || {}
}

四、demo源码

import React, { Component } from 'react'
import {
    DeviceEventEmitter,
    StyleSheet, 
} from 'react-native'
import { WebView } from 'react-native-webview'
import NavigatorBar from '../../components/NavigatorBar'
import { SafeAreaView } from '../../components'

export default class WebViewPage extends Component {

    state = {
        title: '',
        showBackAction: false,
    }

    _autoCheckNavigationBar = nativeEvent => {
        const { canGoBack } = nativeEvent
        this.setState({ showBackAction: canGoBack })
        DeviceEventEmitter.emit('showTabBar', !canGoBack)
    }

    onLoadStart = ({ nativeEvent }) => {
        console.log('onLoadStart', nativeEvent)
    }

    onError = ({ nativeEvent }) => {
        console.log('onError', nativeEvent)
    }

    onLoad = ({ nativeEvent }) => {
        this.setState({ title: nativeEvent.title })
        console.log('onLoad', nativeEvent)
    }

    onLoadEnd = ({ nativeEvent }) => {
        console.log('onLoadEnd', nativeEvent)
        this._autoCheckNavigationBar(nativeEvent)
    }

    renderLoading = () => {
        console.log('renderLoading')
    }

    renderError = ({ nativeEvent }) => {
        console.log('renderError', nativeEvent)
    }

    handleMessage = async ({ nativeEvent }) => {
        const { actionType, extra } = nativeEvent.data && JSON.parse(nativeEvent.data) || {}
    }

    render() {
        const { showBackAction } = this.state
        const { url } = this.props || {}
        return (
            <SafeAreaView style={styles.container} hideBottomView={!showBackAction}>
                <NavigatorBar
                    backAction={showBackAction && this.webViewRef.goBack}
                    title={this.state.title}
                />
                <WebView
                    ref={ref => this.webViewRef = ref}
                    // source={{ uri: url }}
                    source={{ html }}
                    javaScriptEnabled
                    useWebKit
                    allowFileAccess
                    startInLoadingState
                    onLoadStart={this.onLoadStart}
                    onError={this.onError}
                    onLoad={this.onLoad}
                    onLoadEnd={this.onLoadEnd}
                    renderLoading={this.renderLoading}
                    renderError={this.renderError}
                    javaScriptCanOpenWindowsAutomatically
                    onMessage={this.handleMessage}
                    injectedJavaScript={`(function() {window.postMessage = function(data) {window.ReactNativeWebView.postMessage(data)}})();`}
                />
            </SafeAreaView>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
    },
})

const html = `
<!DOCTYPE html>
<html lang="en">
<head>
    <title>测试页面</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div style="text-align: center">
    <button id="viewPicture">1,viewPicture</button>
    <p style="text-align: center">receive react native data: <span id="data"></span></p>
</div>
<script>
    window.onload = function () {
        /* 监听rn消息
        ------------------------------------------------------------------------------------------------------------- */
        const eventListener = nativeEvent => {
            const {actionType, extra} = nativeEvent.data && JSON.parse(nativeEvent.data) || {}
        }
        //安卓用document,ios用window
        window.addEventListener('message', eventListener);
        document.addEventListener('message', eventListener);

        /* 发消息给rn
        ------------------------------------------------------------------------------------------------------------- */
        // 1、查看大图
        document.getElementById('viewPicture').onclick = function () {
            const action = {
                actionType: 'viewPicture',
                extra: {
                    pics: ['https://tva1.sinaimg.cn/large/e6c9d24ely1h0svk2jlcej20ps11ewld.jpg'],
                    defaultIndex: 0,
                },
            }
            window.postMessage(JSON.stringify(action));
        }
    }
</script>
</body>
</html>
`
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流星雨在线

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值