Check internet connection on iOS app with Cordova Phonegap 3.3.0 not working(在 Cordova Phonegap 3.3.0 不工作的情况下检查 iOS 应用程序上的互联网连接)
问题描述
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!
这篇关于在 Cordova Phonegap 3.3.0 不工作的情况下检查 iOS 应用程序上的互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Cordova Phonegap 3.3.0 不工作的情况下检查 iOS 应用程序上的互联网连接


- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 拆分 Drawable 2022-01-01