|
1 | 1 | # react-native-webview
|
2 | 2 | android webview for react-native
|
| 3 | + |
| 4 | +## Installation and How to use |
| 5 | + |
| 6 | +#### Step 1 - NPM Install |
| 7 | + |
| 8 | +```shell |
| 9 | +npm install --save react-native-webview |
| 10 | +``` |
| 11 | +#### Step 2 - Update Gradle Settings |
| 12 | + |
| 13 | +```gradle |
| 14 | +// file: android/settings.gradle |
| 15 | +... |
| 16 | +
|
| 17 | +include ':reactwebview', ':app' |
| 18 | +project(':reactwebview').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-webview') |
| 19 | + // if there are more library |
| 20 | + // include ':app' , ':libraryone' , ':librarytwo' , 'more...' |
| 21 | + // project(':libraryonename').projectDir = new File(rootProject.projectDir, '../node_modules/libraryonemodule') |
| 22 | + // project(':librarytwoname').projectDir = new File(rootProject.projectDir, '../node_modules/librarytwomodule') |
| 23 | + // more.. |
| 24 | +``` |
| 25 | + |
| 26 | +#### Step 3 - Update app Gradle Build |
| 27 | + |
| 28 | +```gradle |
| 29 | +// file: android/app/build.gradle |
| 30 | +... |
| 31 | +
|
| 32 | +dependencies { |
| 33 | + ... |
| 34 | + compile project(':reactwebview') |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +#### Step 4 - Register React Package |
| 39 | + |
| 40 | +```java |
| 41 | +... |
| 42 | +import com.heng.wheel.WheelPackage; |
| 43 | + |
| 44 | +public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { |
| 45 | + |
| 46 | + private ReactInstanceManager mReactInstanceManager; |
| 47 | + private ReactRootView mReactRootView; |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void onCreate(Bundle savedInstanceState) { |
| 51 | + super.onCreate(savedInstanceState); |
| 52 | + mReactRootView = new ReactRootView(this); |
| 53 | + mReactInstanceManager = ReactInstanceManager.builder() |
| 54 | + .setApplication(getApplication()) |
| 55 | + .setBundleAssetName("index.android.bundle") |
| 56 | + .setJSMainModuleName("index.android") |
| 57 | + .addPackage(new MainReactPackage()) |
| 58 | + .addPackage(new WebViewPackage()) // register webview package |
| 59 | + .setUseDeveloperSupport(BuildConfig.DEBUG) |
| 60 | + .setInitialLifecycleState(LifecycleState.RESUMED) |
| 61 | + .build(); |
| 62 | + ... |
| 63 | + } |
| 64 | +... |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | +#### Step 5 - Require and use in Javascript |
| 69 | + |
| 70 | +```js |
| 71 | +// file: index.android.js |
| 72 | + |
| 73 | +var React = require('react-native'); |
| 74 | +var { |
| 75 | + AppRegistry, |
| 76 | + StyleSheet, |
| 77 | + ToastAndroid, |
| 78 | +} = React; |
| 79 | + |
| 80 | +var WebView = require('react-native-webview'); |
| 81 | + |
| 82 | +var WebViewTest = React.createClass({ |
| 83 | + goBack: function() { |
| 84 | + this.refs.webview.goBack(); |
| 85 | + }, |
| 86 | + goForward: function() { |
| 87 | + this.refs.webview.goForward(); |
| 88 | + }, |
| 89 | + reload: function() { |
| 90 | + this.refs.webview.reload(); |
| 91 | + }, |
| 92 | + _onNavigationStateChange: function(event) { |
| 93 | + ToastAndroid.show(event.canGoBack + '',ToastAndroid.SHORT); |
| 94 | + ToastAndroid.show(event.canGoForward + '',ToastAndroid.SHORT); |
| 95 | + ToastAndroid.show(event.url ,ToastAndroid.SHORT); |
| 96 | + ToastAndroid.show(event.title ,ToastAndroid.SHORT); |
| 97 | + ToastAndroid.show(event.loading + '',ToastAndroid.SHORT); |
| 98 | + }, |
| 99 | + render: function() { |
| 100 | + var reqUrl = "https://github.com/"; |
| 101 | + return ( |
| 102 | + <WebView |
| 103 | + ref='webview' |
| 104 | + automaticallyAdjustContentInsets={true} |
| 105 | + url={reqUrl} |
| 106 | + javaScriptEnabledAndroid={true} |
| 107 | + onNavigationStateChange={this._onNavigationStateChange} |
| 108 | + style={styles.webview}/> |
| 109 | + ); |
| 110 | + } |
| 111 | +}); |
| 112 | + |
| 113 | +var styles = StyleSheet.create({ |
| 114 | + webview: { |
| 115 | + flex: 1, |
| 116 | + }, |
| 117 | +}); |
| 118 | + |
| 119 | +... |
| 120 | + |
| 121 | +``` |
| 122 | + |
| 123 | +## Notes |
| 124 | + |
| 125 | +- Only in the following versions tested , other versions do not guarantee success |
| 126 | +```gradle |
| 127 | +// file: react-native-wheel/build.gradle |
| 128 | +
|
| 129 | +android { |
| 130 | + compileSdkVersion 23 //@ |
| 131 | + buildToolsVersion "23.0.1" //@ |
| 132 | +
|
| 133 | + defaultConfig { |
| 134 | + minSdkVersion 16 |
| 135 | + targetSdkVersion 22 //@ |
| 136 | + } |
| 137 | +} |
| 138 | +
|
| 139 | +dependencies { |
| 140 | + compile 'com.facebook.react:react-native:0.16.1' //@ |
| 141 | +} |
| 142 | +
|
| 143 | +
|
| 144 | +``` |
0 commit comments