Javascript 在Cordova Phonegap 3.3.0不工作的情况下检查iOS应用程序上的互联网连接
我试过按照科尔多瓦文档的指南操作,但似乎不起作用 这是我的密码: 我已将Javascript 在Cordova Phonegap 3.3.0不工作的情况下检查iOS应用程序上的互联网连接,javascript,ios,cordova,connection,Javascript,Ios,Cordova,Connection,我试过按照科尔多瓦文档的指南操作,但似乎不起作用 这是我的密码: 我已将添加到config.xml 将此脚本添加到我的index.html: <script type="text/javascript"> document.addEventListener("deviceready", onDeviceReady, false); // device APIs are available // functi
添加到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>
有人知道如何解决这个问题吗
干杯检查您是否已将Cordova.js文件包含在html中
<script type="text/javascript" src="cordova.js"></script>
这对我很有用:
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代码>?我终于解决了这个问题-从头开始重新执行以下操作:
命令行:
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>
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//设备API可用
函数ondevicerady(){
if(navigator.network.connection.type==connection.NONE){
警报(“nocon”);
}否则{
警报(“yescon”);
}
}
然后在iPhone/iPad模拟器中运行它,如果有连接,它将输出“yescon”,如果没有连接,它将输出“nocon”
希望这有帮助 您是否将Cordova.js
包含在您的index.html
文件中是的,我的
Cordova.plist已停止并被替换为我的config.xml,上面的代码与插件代码基本相同我已尝试使用navigator.network.connection.type,Cordova Docs页面底部(适用于3.3.0版)说,我认为这被低估了,或者不适用于iOS——iOS的怪癖iOS无法检测到蜂窝网络连接的类型。navigator.connection.type对于所有手机数据都设置为connection.CELL。它显示的是connection.CELL_2G
,我不确定这是否重要。在重建应用程序并通过www文件复制后,它仍然不起作用。。当你说你让它工作时,你使用的是cordova 3.3.0吗?我使用的是cordova 2.9.hhmmm奇怪,我现在让它输出错误:error:Plugin'NetworkStatus'找不到,或者不是CDVPlugin。在config.xml中检查插件映射:(我在config.xml
中有这一行…太困惑了为什么这不起作用了!Cordova文档太糟糕了..真的需要重写一个更简单的方法,不需要从头开始-不起作用我正在使用ios7和Cordova 3.3使用connection.CELL我没有使用connection.CELL,我检查了是否有连接。
<feature name="NetworkStatus">
<param name="ios-package" value="CDVConnection" />
</feature>
<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>