Android WebView loads local static resources and font summary

Android WebView loads local static resources and font summary

  1. The server sends a zip package of static resources commonly used by H5, downloads it locally and decompresses it
  2. WebView loads the html file in the package and rewrites the WebViewClient
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)

Method, intercept the request, and return WebResourceResponse after intercepting and matching the url, otherwise return null

  1. H5 needs to load local fonts, you can use the file protocol, such as
// H5端
@font-face {
		font-family: 'xxx';
		src: url('file:///android_asset/fonts/xxx.ttf');
}

Just put the font files in the assets/fonts folder on the Android side.
If the font file is too large, you can also put the font file in the sd directory, or the server can send the font file, and then the front end can modify the path accordingly, for example
, put the font file in
storage/emulated/0/WebResource on the Android side The
front end of /fonts/xxx.ttf can be modified accordingly

@font-face {
		font-family: 'xxx';
		src: url('file:///storage/emulated/0/WebResource/fonts/xxx.ttf');
}

The Android side does not need to do any interception or other processing, you can let WebView load local font files

Problems encountered

  1. Passing String through jsb encounters stack overflow error. The reason is that Android uses JsonObject to String. When the data is too large, JsonObject will throw a stack overflow error.
    Solution: jsb passes Object directly

  2. The front-end cannot call the interface.
    Solution: The front-end tells the client to request the interface and request headers through jsb, and the
    client calls the interface and returns the data to the front-end

  3. iOS and Android do not have a unified method of loading local fonts. The
    solution: two packages on the front end

Guess you like

Origin blog.csdn.net/u011656025/article/details/112802591