浏览代码

add WebViewAssetLoader

duxiaofeng-github 4 年前
父节点
当前提交
fc9868accc
共有 2 个文件被更改,包括 19 次插入0 次删除
  1. 1
    0
      android/build.gradle
  2. 18
    0
      android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java

+ 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
     }