我尝试过以下this http://cordova.apache.org/docs/en/3.3.0/cordova_connection_connection.md.html#The%20Command-Line%0AInterfaceCordova 文档指南,但它似乎不起作用。
这是我的代码:
我已经添加了<plugin name="NetworkStatus" value="CDVConnection" />
to 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
有人知道如何解决这个问题吗?
Cheers
我终于解决了问题!!- 从头开始并执行以下操作:
命令行:
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/
folder.
Open up hello.xcodeproj
在 Xcode 中。
编辑 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”!
希望这可以帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)