【发布时间】:2014-01-08 01:29:47
【问题描述】:
我已尝试按照 Cordova 文档上的 this 指南进行操作,但它似乎不起作用。
这是我的代码:
我已将<plugin name="NetworkStatus" value="CDVConnection" /> 添加到config.xml。
这个脚本给我的index.html:
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
alert("1"); // runs this alert
checkConnection();
}
function checkConnection() {
var networkState = Connection.CELL;
alert("2"); // doesn't run this
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
</script>
var networkState = Connection.CELL; 似乎是导致问题的原因,因为它没有运行以下警报,我也尝试过navigator.connection.type,但发生了同样的事情。
当我在 Chrome 中运行应用程序时,控制台输出以下错误:
Uncaught ReferenceError: Connection is not defined
有人知道如何解决这个问题吗?
干杯
【问题讨论】:
-
您是否将
Cordova.js包含在您的index.html文件中 -
是的,我有
<script type="text/javascript" src="cordova.js"></script>
标签: javascript ios cordova connection