cordova 5.1.1的互联网检测

Internet detection on cordova 5.1.1

本文关键字:检测 互联网 cordova      更新时间:2023-09-26

我使用的是phonegap 2.5.0,刚刚迁移到cordova 5.1.1。在2.5.0中,我使用了下面的脚本来检查互联网连接

<script type="text/javascript" src="cordova-2.5.0.js"></script>
    <script type="text/javascript" charset="utf-8">
    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);
    // PhoneGap is loaded and it is now safe to make calls PhoneGap methods
    //
    function onDeviceReady() {
        checkConnection();
    }
    function checkConnection() {
        var networkState = navigator.network.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.NONE]     = 'No network connection';
        if ((states[networkState]) == states[Connection.NONE])
        $('#notification').fadeIn("slow", function(){
            $("#notification").fadeOut(7000);
        });

    }
    </script>

但是当我迁移到5.1.1时,脚本无法工作。虽然我已经替换了<script type="text/javascript" src="cordova-2.5.0.js"></script><script type="text/javascript" src="cordova.js"></script>,我有cordova.js在我的根文件夹,这是www

我建议使用插件,请阅读:https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin

使用:

cordova plugin add nl.x-services.plugins.socialsharing