检查与cordovaPhonegap 3.3.0 iOS应用程序无法正常工作的互联网连接

我已经尝试按照Cordova docs上的这个指南,但它似乎不工作。

这是我的代码:

我已将<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”!

希望这可以帮助!

检查你是否包含你的Cordova.js文件在HTML中。

 <script type="text/javascript" src="cordova.js"></script> 

并更改您的项目的App/Supporting Files/Cordova.plist

 <key>Plugins</key> <dict> <key>NetworkStatus</key> <string>CDVConnection</string> </dict> 

这适用于我:

 if(navigator.network.connection.type == Connection.NONE){ //no connection }else{ //You are connected. } 

虽然我看文档,看起来像使用这些行有所不同:

var networkState = navigator.network.connection.type;

对于所有蜂窝数据,navigator.network.connection.type设置为Connection.CELL_2G

它会是var networkState = Connection.CELL_2G;