我有一个包含页面 A 的 WebView。 页面 A 有一个包含页面 B 的 iframe。 页面 A 和 B 都在应用的 Assets 中。
页面 A 有一个消息事件处理程序,页面 B 调用 parent.postMessage
但我遇到了安全问题:
I/chromium(997): [INFO:CONSOLE(27)] "Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.", source: file:///android_asset/path/to/my.js
我假设 origins 为空,因为文件是 Assets ,但有没有办法让它们以这种方式进行通信?
最佳答案
您应该在页面内启用文件访问。使用此设置:
webView.getSettings().setAllowFileAccessFromFileURLs(true);
编辑:
此设置从 SDK 版本 16 开始有效。
关于javascript - 来自 iframe 的 Android WebView postMessage 和 Assets 中的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24850692/