duxiaofeng 4 年之前
父節點
當前提交
38c50ffc0a
No account linked to committer's email address
共有 3 個文件被更改,包括 33 次插入0 次删除
  1. 1
    0
      android/build.gradle
  2. 18
    0
      android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java
  3. 14
    0
      docs/Guide.md

+ 1
- 0
android/build.gradle 查看文件

@@ -134,4 +134,5 @@ dependencies {
134 134
   //noinspection GradleDynamicVersion
135 135
   implementation 'com.facebook.react:react-native:+'
136 136
   implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
137
+  implementation 'androidx.webkit:webkit:1.2.0'
137 138
 }

+ 18
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java 查看文件

@@ -43,6 +43,9 @@ import androidx.annotation.Nullable;
43 43
 import androidx.annotation.RequiresApi;
44 44
 import androidx.core.content.ContextCompat;
45 45
 import androidx.core.util.Pair;
46
+import androidx.webkit.WebViewAssetLoader;
47
+import androidx.webkit.WebViewAssetLoader.ResourcesPathHandler;
48
+import androidx.webkit.WebViewAssetLoader.AssetsPathHandler;
46 49
 
47 50
 import com.facebook.common.logging.FLog;
48 51
 import com.facebook.react.views.scroll.ScrollEvent;
@@ -149,6 +152,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
149 152
   protected @Nullable String mUserAgent = null;
150 153
   protected @Nullable String mUserAgentWithApplicationName = null;
151 154
 
155
+  private static WebViewAssetLoader assetLoader;
156
+
152 157
   public RNCWebViewManager() {
153 158
     mWebViewConfig = new WebViewConfig() {
154 159
       public void configWebView(WebView webView) {
@@ -196,6 +201,13 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
196 201
     }
197 202
     setMixedContentMode(webView, "never");
198 203
 
204
+    if (assetLoader == null) {
205
+      assetLoader = new WebViewAssetLoader.Builder()
206
+        .addPathHandler("/assets/", new AssetsPathHandler(reactContext))
207
+        .addPathHandler("/res/", new ResourcesPathHandler(reactContext))
208
+        .build();
209
+    }
210
+
199 211
     // Fixes broken full-screen modals/galleries due to body height being 0.
200 212
     webView.setLayoutParams(
201 213
       new LayoutParams(LayoutParams.MATCH_PARENT,
@@ -778,6 +790,12 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
778 790
     protected RNCWebView.ProgressChangedFilter progressChangedFilter = null;
779 791
     protected @Nullable String ignoreErrFailedForThisURL = null;
780 792
 
793
+    @Override
794
+    public WebResourceResponse shouldInterceptRequest(WebView view,
795
+                                                      WebResourceRequest request) {
796
+      return assetLoader.shouldInterceptRequest(request.getUrl());
797
+    }
798
+
781 799
     public void setIgnoreErrFailedForThisURL(@Nullable String url) {
782 800
       ignoreErrFailedForThisURL = url;
783 801
     }

+ 14
- 0
docs/Guide.md 查看文件

@@ -89,6 +89,20 @@ class MyWeb extends Component {
89 89
 }
90 90
 ```
91 91
 
92
+**Access local assets by WebViewAssetLoader on android x**
93
+
94
+Android x provide a new way to access assets, by using WebViewAssetLoader. now we can access assets via uri **https://appassets.androidplatform.net/assets/some-assets**
95
+
96
+For example:
97
+
98
+```tsx
99
+<WebView
100
+  source={{
101
+    uri: 'https://appassets.androidplatform.net/assets/index.html',
102
+  }}
103
+/>
104
+```
105
+
92 106
 </details>
93 107
 
94 108
 ### Controlling navigation state changes