ReactNative Webview交互

RN 端:

    onMessage = (event) => {
        let paramArray=event.nativeEvent.data.split(',');
        
        //用户收取途链
        let params=new Map();
        params.set('telPhone',paramArray[0]);
        params.set('context',paramArray[1]);
        Http.write(2000,params,(promise)=> {
            promise.then(response => {
                this.refs.toast.show(response.message)
            }).catch(err => {
                //请求失败
                this.refs.toast.show(err)
            })
        })
    }
                <WebView
                    ref='webViewRef'
                    automaticallyAdjustContentInsets={false}
                    style={styles.webView}
                    source={{uri: ''}}
                    javaScriptEnabled={true}
                    domStorageEnabled={true}
                    decelerationRate="normal"
                    onNavigationStateChange={this.onNavigationStateChange}
                    onShouldStartLoadWithRequest={this.onShouldStartLoadWithRequest}
                    // startInLoadingState={true}
                    scalesPageToFit={this.state.scalesPageToFit}
                    onMessage={this.onMessage}
                />

Web端:

<input type="button" onclick="toNative()" id="tjfk" value="提交意见">
    function toNative() {
        let context=window.document.getElementById('studyIdea').value;
        let phone=window.document.getElementById('phone').value;
        window.postMessage([context,phone]);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容