2016-10-20 58 views
0

Currenlty我在一个cordova应用程序中嵌入在线网站;我config.xml中如下:允许iframe执行cordova命令

<plugin name="cordova-plugin-whitelist" spec="1" /> 
<access origin="*" /> 
<allow-intent href="http://*/*" /> 
<allow-intent href="https://*/*" /> 
<allow-intent href="tel:*" /> 
<allow-intent href="sms:*" /> 
<allow-intent href="mailto:*" /> 
<allow-intent href="geo:*" /> 
<allow-navigation href="*" /> 

和我的index.html有以下meta标签:

<meta http-equiv="Content-Security-Policy" content="default-src *; 
style-src * 'self' 'unsafe-inline' 'unsafe-eval'; 
script-src * 'self' 'unsafe-inline' 'unsafe-eval';"> 

现在我需要执行一个命令codova(应用程序)的从内部网站,我用下面的代码:

window.parent.cordova.plugins.barcodeScanner.scan(function (result) {}, 
function (error) {}); 

但它的(正确)与一个失败的:

Uncaught SecurityError: Blocked a frame with origin "http://" from accessing a frame with origin "file://". The frame requesting access has a protocol of "http", the frame being accessed has a protocol of "file". Protocols must match.

如何允许从外部网站到我的应用程序的这种交互?

回答

0

由于CORS和沙箱iframe是定义不正确的方式来处理这种通信。

在科尔多瓦有一个名为InAppBrowser的插件;我们可以安装它:

cordova plugin add cordova-plugin-inappbrowser

,它允许我们使用executeScript方法以一种messagging的浏览器窗口交互:

var myRef = cordova.InAppBrowser.open('http://<site>', '_blank', 'location=no'); 

myRef.executeScript({ code: "localStorage.setItem('demo', '');" });