在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:alinz/react-native-webview-bridge开源软件地址:https://github.com/alinz/react-native-webview-bridge开源编程语言:Objective-C 48.0%开源软件介绍:issue firstPlease take a look at thisReact Native WebView Javascript BridgeI have been testing and reading a lot of way to safely create a bridge between react-native and webview. I'm happy to announced that the wait is over and from React-Native 0.20 and above, the bridge is fully functional. InstallationIn order to use this extension, you have to do the following steps: in your react-native project, run iOS
Android
import com.github.alinz.reactnativewebviewbridge.WebViewBridgePackage;
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new WebViewBridgePackage() //<- this
);
}
Usagejust import the module with one of your choices way: ** CommonJS style ** var WebViewBridge = require('react-native-webview-bridge'); ** ES6/ES2015 style ** import WebViewBridge from 'react-native-webview-bridge';
sendToBridge(message)the message must be in string. because this is the only way to send data back and forth between native and webview. onBridgeMessagethis is a prop that needs to be a function. it will be called once a message is received from webview. The type of received message is also in string. allowFileAccessFromFileURLs (Android only)this is a prop that allows locally loaded pages via file:// to access other file:// resources. Pass-thru to corresponding setting in WebView. Default is allowUniversalAccessFromFileURLs (Android only)this is a prop that allows locally loaded pages via file:// to access resources in any origin. Pass-thru to corresponding setting in WebView. Default is Bridge Scriptbridge script is a special script which injects into all the webview. It automatically register a global variable called send(message)this method sends a message to native side. the message must be in string type or onMessagethis method needs to be implemented. it will be called once a message arrives from native side. The type of message is in string. onError (iOS only)this is an error reporting method. It will be called if there is an error happens during sending a message. It receives a error message in string type. Notes
Simple ExampleThis example can be found in const injectScript = `
(function () {
if (WebViewBridge) {
WebViewBridge.onMessage = function (message) {
if (message === "hello from react-native") {
WebViewBridge.send("got the message inside webview");
}
};
WebViewBridge.send("hello from webview");
}
}());
`;
var Sample2 = createReactClass({
onBridgeMessage(message){
const { webviewbridge } = this.refs;
switch (message) {
case "hello from webview":
webviewbridge.sendToBridge("hello from react-native");
break;
case "got the message inside webview":
console.log("we have got a message from webview! yeah");
break;
}
},
render() {
return (
<WebViewBridge
ref="webviewbridge"
onBridgeMessage={this.onBridgeMessage.bind(this)}
injectedJavaScript={injectScript}
source={{uri: "https://google.com"}}/>
);
}
}); |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论