Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot load local resources from WebView #1

Open
marcprux opened this issue Apr 3, 2024 · 0 comments
Open

Cannot load local resources from WebView #1

marcprux opened this issue Apr 3, 2024 · 0 comments

Comments

@marcprux
Copy link
Contributor

marcprux commented Apr 3, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant