首页 > 解决方案 > Cordova Network Information Plugin doesn't work properly

问题描述

I have built a Mobile application with Cordova, Onsen UI and Vue.js. While working with the network connectivity, I have installed the cordova plugin

cordova plugin add cordova-plugin-network-information

then to check the type of the connection I have used the following instruction

var network = navigator.connection.type
alert(network);

But it shows undefined. After printing navigator.connection in the console log it shows the following properties

  1. downlink
  2. effectiveType
  3. onchange
  4. rtt

But When I use the following instruction

navigator.connection.effectiveType

it shows the type.

Another problem is that when I don't have the internet connection the effecticeType property shows 3g/4g.

Why is that? Then how can I get the confirmation that there is no internet connection? Please Help me out.

标签: javascriptcordovavue.js

解决方案


假设你已经正确安装了插件,下面是我用来检查 WiFi 或数据连接是否处于活动状态的函数:

function checkConnection() {
    console.log('checkConnection()');

    if(typeof (window.cordova) === 'undefined') {
        return true; // we are on the browser
    } else if(navigator.connection.type == Connection.NONE) {
        return false; // offline
    } else {
        return true; // online
    }
}

推荐阅读