我有一个网站,我想加强手机上的混合应用程序。理想情况下,应用程序可以通过http加载远程站点,但在本地加载平台特定的代码。我试过建立一个科多瓦项目来做这件事,但我遇到了很多麻烦。Cordova 4.3.1,Cordova Android 3.7.2上的Nexus 7 Android 4.4.4一直是我的环境。(谷歌地图插件用于Cordova Android <4。)
如果我尝试从http站点加载file:///android_assets/www/cordova.js,铬表示Not allowed to load local resource
。我试图建立一个本地的URL方案,但不知道哪些特定于android的代码要放在哪里。
这是制作混合应用程序的正确方法吗?是否有允许加载文件的cordova插件:来自http:?
我还尝试使用iframe和传递消息来指示cordova已启用和安装了哪些插件,但我不想处理让http服务器重新服务本地已有的js文件的问题。
我还想下载该网站并通过文件访问它:但我想我在访问http资源时也会遇到同样的问题。
发布于 2015-06-24 06:42:53
我不知道这是否是制作混合应用程序的正确方法,但我通过使用https://github.com/floatinghotpot/cordova-httpd在http上提供资产来实现它的工作。
在与我添加的应用程序捆绑在一起的本地页面上:
<script type="text/javascript">
document.addEventListener("deviceready", function () {
var httpd = cordova.plugins.CorHttpd;
httpd.startServer({
'www_root': '',
'port': 8080,
'localhost_only': false
}, function (url) {
// if server is up, it will return the url of http://<server ip>:port/
// the ip is the active network connection
// if no wifi or no cell, "127.0.0.1" will be returned.
document.getElementById('url').innerHTML = "server is started: <a href='" + url + "' target='_blank'>" + url + "</a>";
location.replace('http://192.168.0.134:9090/homechooser/beta/#' + JSON.stringify({'cordova.js': url + '/cordova.js'}));
}, function (error) {
document.getElementById('url').innerHTML = 'failed to start server: ' + error;
});
}, false);
</script>
<div id="url"></div>
然后我在远程网页上添加了:
(function () {
var hash;
if (location && location.hash)
try {
hash = JSON.parse(location.hash.substring(1));
} catch (e) {
}
if (hash && hash['cordova.js'])
$.getScript(hash['cordova.js'], function () {
alert("Running cordova.js");
});
})();
现在看看科多瓦是否真的可以这样使用..。
发布于 2017-12-04 11:39:21
我能够使用cordova- plugin (cdvfile://
)插件在远程html上加载本地cordova.js
。
cordova.js
的cdvfile URI (Android示例)
checkCordovaJSPath('file:///android_asset/www/cordova.js');
function checkCordovaJSPath(jsUri) {
window.resolveLocalFileSystemURL(jsUri, function success(fileEntry){
console.log("got cordova.js file: " + fileEntry.fullPath);
console.log('cordova.js cdvfile URI: ' + fileEntry.toInternalURL());
});
}
cdvfile://localhost
加载cdvfile://localhost
。
<script type="text/javascript" src="cdvfile://localhost/assets/www/cordova.js"/>
发布于 2019-03-03 22:34:28
由于混合内容策略,cdvfile解决方案将不适用于活动内容(js文件)。下面是如何使它工作的方法:
对于android:通过将以下代码放入cordova插件的pluginInitialize方法中禁用混合内容策略:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
final WebSettings settings = ((WebView)this.webView.getView()).getSettings();
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
允许)
然后使用以下方法包括本地cordova.js:
<script src="cdvfile://localhost/assets/www/cordova.js"></script>
对于ios:我向文件插件提交了一个PR,该插件解决了ios: apache/cordova- plugin #296上的混合内容问题,如果您在webview上加载远程站点file://localhost/bundle/www/cordova.js,那么它允许使用url:https://example.com/cdvfile/bundle/www/cordova.js而不是cdv https://github.com/guylando/cordova-plugin-file访问本地文件,从而解决了混合内容问题。
使用以下方法包括本地cordova.js:
<script src="/cdvfile/bundle/www/cordova.js"></script>
https://stackoverflow.com/questions/31017518
复制相似问题