javascript – 检查iOS应用程序上的互联网连接,Cordova Phonegap 3.3.0无效

我已经尝试过关于Cordova docs的 this

指南,但它似乎不起作用.

这是我的代码:

我添加了<plugin name =“NetworkStatus”value =“CDVConnection”/>到config.xml.

这个脚本到我的index.html:

<script type="text/javascript">


        document.addEventListener("deviceready", onDeviceReady, false);

        // device APIs are available
        //
        function onDeviceReady() {
            alert("1"); // runs this alert
            checkConnection();
        }

        function checkConnection() {
            var networkState = Connection.CELL;
            alert("2"); // doesn't run this

            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]);
        }

    </script>

var networkState = Connection.CELL;似乎导致问题,因为它没有运行以下警报,我也尝试过navigator.connection.type,但同样的事情发生了.

当我在Chrome中运行应用时,控制台会输出以下错误:

Uncaught ReferenceError: Connection is not defined

有谁知道如何解决这个问题?

干杯

我终于解决了这个问题!! – 从头开始​​重新开始并执行以下操作:

命令行:

sudo npm install -g cordova
cordova create hello com.example.hello HelloWorld
cd hello
cordova platform add ios
cordova platforms ls //This will list ios
cordova plugin add org.apache.cordova.network-information
cordova build

然后将我的文件(HTML,Javascript等)拖到platforms / ios / www /文件夹中.

在xcode中打开hello.xcodeproj.

编辑config.xml并添加以下行:

<feature name="NetworkStatus">
    <param name="ios-package" value="CDVConnection" />
</feature>

然后在我的索引文件中我使用了JavaScript:

<script type="text/javascript">
        document.addEventListener("deviceready", onDeviceReady, false);
        // device APIs are available
        function onDeviceReady() {
            if(navigator.network.connection.type == Connection.NONE){
                alert("nocon");
            }else{
                alert("yescon");
            }
        }
    </script>

然后在iPhone / iPad模拟器中运行它,如果有连接则输出“yescon”,如果没有则输出“nocon”!!

希望这可以帮助!

翻译自:https://stackoverflow.com/questions/20682388/check-internet-connection-on-ios-app-with-cordova-phonegap-3-3-0-not-working

我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章