【问题标题】:Cannot read the property 'type' of undefined error in cordova network plugin无法读取科尔多瓦网络插件中未定义错误的属性“类型”
【发布时间】:2014-12-15 00:43:11
【问题描述】:
【问题讨论】:
标签:
javascript
android
cordova
cordova-plugins
【解决方案1】:
由于某种原因,navigator.connection 对象没有像cordova一样同时准备好。
您必须使用超时,例如这个 - 为 Angular/Ionic 设计的
setTimeout(function(){
var connection = connectionService.getConnection();
console.log("connection : "+connection);
$scope.connection = connection;
$scope.$apply()
}, 5000)
【解决方案2】:
用我的
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
checkConnection()
}
function checkConnection() {
networkState = navigator.connection.type;
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]);
}