【发布时间】:2020-05-13 16:58:29
【问题描述】:
在某些设备上的 Webview 上加载 URL 时我遇到了一些问题:例如一般的模拟器(Nexus 5、Android API 21)和真实设备(华为 P30、Android 10)。
编辑:我使用的是 minSDK 19。网址是 https 方案。
binding.webview.apply {
settings.javaScriptEnabled = true
settings.loadWithOverviewMode = true
settings.domStorageEnabled = true
settings.databaseEnabled = true
settings.setRenderPriority(WebSettings.RenderPriority.HIGH)
settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
settings.setAppCacheEnabled(true)
settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
settings.useWideViewPort = true
settings.enableSmoothTransition()
webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
writeData(getString(XXX).toString())
super.onPageStarted(view, url, favicon)
binding.layoutProgress.progressLayout.show()
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
binding.layoutProgress.progressLayout.invisible()
}
}
}
我正在 localStorage 上编写需要通过 javascript 浏览器才能加载的内容。我不确定这是不是问题...
private fun writeData(value: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
binding.webview.evaluateJavascript(
"localStorage.setItem('$XXX','$value');",
) { returnedValue -> Log.d("chromium1", "onReceiveValue: $returnedValue") } //returns null on the devices that does not work or value on the working devices
} else {
binding.webview.loadUrl("javascript:localStorage.setItem('$XXX','$value');")
}
}
在未加载的设备上出现的chromium 错误是:
"Uncaught SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.",
"Unrecognized Content-Security-Policy directive 'navigate-to'. ", source: url.com (0)
"Unrecognized Content-Security-Policy directive 'navigate-to'. ", source: url.com (0)
"The key "shrink-to-fit" is not recognized and ignored.", source: url.com (5)
"Refused to load the stylesheet 'https://fonts.googleapis.com/css?family=Source+Code+Pro:300,600&display=swap' because it violates the following Content Security Policy directive: "style-src 'self' 'unsafe-inline' https://widget.freshworks.com blob:". ", source: url.com (11)
"Uncaught SyntaxError: Use of const in strict mode.", source: url.something.js (21)
非常感谢您。
【问题讨论】:
-
请详细说明“某些设备”以帮助解决问题。它只发生在特定的设备品牌上吗?或者可能在某些 API 版本上?
-
非常感谢。我用一些设备更新了评论。
-
这里只是一个想法,但它可能与您的权限设置有关吗?如果这些未在 android 清单中列出,您将遇到问题。
-
如果您在 Chromium 浏览器中检查其他设置怎么办? stackoverflow.com/questions/30481516/…(和类似链接)。
标签: android kotlin webview chromium