1.0.0 • Published 5 years ago

mf-react-native-webview-jsbridge v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

React Native WebView jsbridge - Complete the Correspondence between web and Native

React Native WebView jsbridge 是在react-native-community/react-native-webview基础上开发的实现的native与web的通信。

支持的平台

  • iOS (支持 UIWebView,WKWebView)
  • Android (使用腾讯X5内核)

自动安装

使用npm

$ npm install mf-react-native-webview-jsbridge -save

或使用yarn

$ yarn add mf-react-native-webview-jsbridge

然后

$ react-native link mf-react-native-webview-jsbridge

##手动安装 ###iOS 1. Libraries文件下右键Add Files to 'xxx'...,添加RNCWebView.xcodeproj. 2. 在Linked Frameworks and Libraries添加libRNCWebView.a

###Android 1. 在你的工程下的android/settings.gradle:

...
include ':mf-react-native-webview-jsbridge'
project(':mf-react-native-webview-jsbridge').projectDir = new File(rootProject.projectDir, '../node_modules/mf-react-native-webview-jsbridge/lib/android')
  1. 在你的工程下的android/app/build.gradle:
...
dependencies {
  ...
  implementation project(':mf-react-native-webview-jsbridge')
}
  1. 添加 import com.airbnb.android.react.maps.MapsPackage;new MapsPackage() 在你的 MainApplication.java
import com.reactnativemf.webview.ReactNativeWebViewPackage;
...
    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
                new MainReactPackage(),
                new ReactNativeWebViewPackage()
        );
    }

使用

Import the WebView component from react-native-webview and use it like so:

import React, { Component } from "react";
import { StyleSheet, Text, View, Dimensions, TouchableOpacity } from "react-native";
import { WebView } from "react-native-webview";

// ...
class MyWebComponent extends Component {
  componentDidMount() {
    /**
     * 参数1:名称
     * 参数2: 传递给js的参数
     * 参数3: 回调函数,获取js返回的参数
     */
    this.webviewRef && this.webviewRef.registerHandler('getJsInfo', 'oc传给了js一条信息', (res)=> {
      Alert.alert(res.msg);
    });
  }
  render() {
    const size = Dimensions.get('window');
    return (
      <View style={styles.container}>
        <TouchableOpacity
          style={{ marginTop: 20 }}
          onPress={this.handPress}
        >
          <Text>App调用Web端提供的方法</Text>
        </TouchableOpacity>
        <WebView
          ref={refs => (this.webviewRef = refs)}
          useWebKit
          style={{ width: size.width, height: size.height }}
          source={{ uri: '' }}
        />
      </View>
    );
  }
  handPress = () => {
    /**
     * 参数1:名称
     * 参数2: 传递给js的参数
     * 参数3: 回调函数,获取js返回的参数
     */
    this.webviewRef.callHandler('getJsMethod', '', (data) => {
      Alert.alert(data);
    });
  }
}

关于webview的API Reference

故障排除

  • 如果你遇到 Invariant Violation: Native component for "RNCWKWebView does not exist" 你可以使用 react-native link 或者手动链接
  • 'config.h' file not foundreplace glog-0.3.X
cd node_modules/react-native/third-party/glog-0.3.4
./configure

License

MIT

1.0.0

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago