检查与cordova的互联网连接是否无法正常工作

时间:2015-06-10 22:54:47

标签: javascript android ios cordova

我正在使用此插件查看我的手机是否有互联网连接: https://github.com/apache/cordova-plugin-network-information

但不工作..告诉我这个错误:

D/CordovaLog( 3169): file:///android_asset/www/index.html: Line 44 : Uncaught TypeError: Cannot read property 'connection' of undefined

我的功能:

```` var App = {

//Application Constructor
init: function() {
    this.bindEvents();
},

//Bind Event Listeners
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

//Event Handle
onDeviceReady: function() {
    //Fast click
    FastClick.attach(document.body);          

    Usuario.verificarOnline();
    DataBase.init();

    alert(App._internet());
},

getUrl: function(){        
    return App.url;
},

//verificar conexao com internet
_internet: function(){
    var networkState = navigator.network.connection.type;

    var states = {};
        states[Connection.UNKNOWN]  = false;
        states[Connection.ETHERNET] = true;
        states[Connection.WIFI]     = true;
        states[Connection.CELL_2G]  = true;
        states[Connection.CELL_3G]  = true;
        states[Connection.CELL_4G]  = true;
        states[Connection.CELL]     = true;
        states[Connection.NONE]     = false;

   return states[networkState];
}

};

````

1 个答案:

答案 0 :(得分:1)

这意味着你没有安装插件:

cordova plugin add cordova-plugin-network-information

或者表示您在调用函数之前没有等待设备准备就绪:

document.addEventListener('deviceready', function()
{
    // Call the function
}