[英]Check internet connection on iOS app with Cordova Phonegap 3.3.0 not working
I have tried following this guide on Cordova docs, but it doesn't seem to work. 我试过在Cordova docs上遵循这个指南,但它似乎不起作用。
Here is my code: 这是我的代码:
I have added <plugin name="NetworkStatus" value="CDVConnection" />
to config.xml
. 我已将<plugin name="NetworkStatus" value="CDVConnection" />
到config.xml
。
and this script to my index.html
: 这个脚本到我的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. 似乎导致问题,因为它没有运行以下警报,我也尝试过navigator.connection.type
但同样的事情发生了。
When I run the app in Chrome the console outputs the following error: 当我在Chrome中运行应用时,控制台会输出以下错误:
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. 然后将我的文件(HTML,Javascript等)拖到platforms/ios/www/
文件夹中。
Open up hello.xcodeproj
in xcode. 在xcode中打开hello.xcodeproj
。
Edit config.xml and add the lines: 编辑config.xml并添加以下行:
<feature name="NetworkStatus">
<param name="ios-package" value="CDVConnection" />
</feature>
Then in my index file I used the JavaScript: 然后在我的索引文件中我使用了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!! 然后在iPhone / iPad模拟器中运行它,如果有连接则输出“yescon”,如果没有则输出“nocon”!!
Hope this helps! 希望这可以帮助!
Check if you have included your Cordova.js file in html. 检查您是否在html中包含了Cordova.js文件。
<script type="text/javascript" src="cordova.js"></script>
And change App/Supporting Files/Cordova.plist
of your project. 并更改项目的App/Supporting Files/Cordova.plist
。
<key>Plugins</key>
<dict>
<key>NetworkStatus</key>
<string>CDVConnection</string>
</dict>
This works for me: 这对我有用:
if(navigator.network.connection.type == Connection.NONE){
//no connection
}else{
//You are connected.
}
Though I look in the documentation and looks like there's a difference using these lines: 虽然我查看了文档,看起来使用这些行有所不同:
var networkState = navigator.network.connection.type;
navigator.network.connection.type is set to Connection.CELL_2G
for all cellular data. 对于所有蜂窝数据,navigator.network.connection.type设置为Connection.CELL_2G
。
Would it be var networkState = Connection.CELL_2G;
它是var networkState = Connection.CELL_2G;
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.