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

栏目: JavaScript · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/20682388/check-internet-connection-on-ios-app-with-cordova-phonegap-3-3-0-not-working
我已经尝试过关于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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to Probability Theory and Its Applications

An Introduction to Probability Theory and Its Applications

William Feller / Wiley / 1991-1-1 / USD 120.00

Major changes in this edition include the substitution of probabilistic arguments for combinatorial artifices, and the addition of new sections on branching processes, Markov chains, and the De Moivre......一起来看看 《An Introduction to Probability Theory and Its Applications》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具