Coder Social home page Coder Social logo

skip-web's Issues

Cannot load local resources from WebView

It is common to want to load a locally-bundled HTML resource and load it in a web view, like so:

WebView(url: Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "about"))

This works in iOS, but on Android the load fails with the error:

The webpage at jar:file:/data/app/~~id/base.apk!/pgk/Resources/about/index.html could not be loaded because: 
net::ERR_UNKOWN_URL_SCHEME

The problem is that the Android web view doesn't automatically handle the jar:file: scheme.

If Skip resources were bundles as Android resources (which they aren't: they are traditional Java resources), then we might be able to transform local file loads into file://android_assets/ or file://android_res/ schemes, but this is expressly recommended against at https://developer.android.com/develop/ui/views/layout/webapps/load-local-content#antipatterns

The correct solution will probably be to implement a WebViewAssetLoader, as per https://developer.android.com/develop/ui/views/layout/webapps/load-local-content#assetloader . This would enable the customization of resource loading, and we could use the same mechanism to implement bespoke content loading schemes (e.g., loading content from a zip archive). Parity with WKWebView could be achieved through the WKNavigationDelegate decidePolicyFor delegate.

For the time being, the only workaround is to manually load the contents of the file into memory and load the HTML, like so:

WebView(html: try String(contentsOf: Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "about")))

This is very limited, though, because peer resources (e.g., scripts, stylesheets, and images) will not be resolved, so the HTML must be completely self-contained.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.