在iOS应用程序上检查互联网连接Cordova Phonegap 3.3.0不工作 [英] Check internet connection on iOS app with Cordova Phonegap 3.3.0 not working

查看:119
本文介绍了在iOS应用程序上检查互联网连接Cordova Phonegap 3.3.0不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试遵循



这是我的代码:



我已将< plugin name =NetworkStatusvalue =CDVConnection/> 添加到 config.xml 。 / p>

并将此脚本添加到我的 index.html

 < script type =text / javascript> 


document.addEventListener(deviceready,onDeviceReady,false);

//设备API可用
//
function onDeviceReady(){
alert(1); //运行此警报
checkConnection();
}

function checkConnection(){
var networkState = Connection.CELL;
alert(2); //不运行这个

var states = {};
状态[Connection.UNKNOWN] ='未知连接';
states [Connection.ETHERNET] ='以太网连接';
states [Connection.WIFI] ='WiFi连接';
states [Connection.CELL_2G] ='手机2G连接';
states [Connection.CELL_3G] ='手机3G连接';
states [Connection.CELL_4G] ='手机4G连接';
states [Connection.CELL] ='单元通用连接';
状态[Connection.NONE] ='无网络连接';

alert('连接类型:'+ states [networkState]);
}

< / script>

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



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

 未捕获ReferenceError:Connection is未定义

任何人都知道如何解决这个问题?



干杯

解决方案

我终于解决了问题!从头开始并执行以下操作:



命令行:

  sudo npm install -g cordova 
cordova create hello com.example.hello HelloWorld
cd hello
cordova平台添加ios
cordova平台ls //这将列出ios
cordova plugin add org.apache.cordova.network-information
cordova build

然后拖动我的文件(HTML,Javascript等)插入到 platforms / ios / www / 文件夹中。



打开 hello.xcodeproj



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

 < feature name =NetworkStatus> 
< param name =ios-packagevalue =CDVConnection/>
< / feature>

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

 < script type =text / javascript> 
document.addEventListener(deviceready,onDeviceReady,false);
//设备API可用
function onDeviceReady(){
if(navigator.network.connection.type == Connection.NONE){
alert(nocon);
} else {
alert(yescon);
}
}
< / script>

然后在iPhone / iPad模拟器中运行它,如果有连接,它将输出yescon



希望这有助于!


I have tried following this guide on Cordova docs, but it doesn't seem to work.

Here is my code:

I have added <plugin name="NetworkStatus" value="CDVConnection" /> to config.xml.

and this script to my 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; seems to cause the problem as it doesn't run the following alert, I have also tried navigator.connection.type but the same thing happened.

When I run the app in Chrome the console outputs the following error:

Uncaught ReferenceError: Connection is not defined 

Anybody know how to solve this problem?

Cheers

解决方案

I finally solved the problem!! - by starting all over again from scratch and doing the following:

Command line:

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

Then drag my files (HTML, Javascript etc) into the platforms/ios/www/ folder.

Open up hello.xcodeproj in xcode.

Edit config.xml and add the lines:

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

Then in my index file I used the 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>

Then run it in the iPhone / iPad simulator and it will output "yescon" if there is a connection and "nocon" if there isn't!!

Hope this helps!

这篇关于在iOS应用程序上检查互联网连接Cordova Phonegap 3.3.0不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆