javascript - 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
- downlink
- effectiveType
- onchange
- 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.
解决方案
假设你已经正确安装了插件,下面是我用来检查 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
}
}
推荐阅读
- twilio - Twilio & ThinQ 问题
- java - 用 y 个数求和 x 的方法数?
- linux - AWS Transcribe through AWS CLI
- c# - 有没有办法向矩形结构添加附加值?
- ruby-on-rails - Rails 验证 ASCII 近似值的唯一性
- css - _ngcontent-c0 being added to a css rule automatically
- amazon-cloudwatch - 用于 splunk 的 AWS 批处理日志
- openssl - OpenSSL pkcs12 export fails when specifying CSR file
- php - SQL injection vulnerability with prepared statement that uses concatenation
- java - NewsAPI Exclude Sources or Websites