ReactNative Webview交互
2018-08-14 本文已影响0人
Windows_XP
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]);
}