Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
73
rated 0 times [  74] [ 1]  / answers: 1 / hits: 24530  / 11 Years ago, thu, december 19, 2013, 12:00:00

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


More From » ios

 Answers
43

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!


[#73657] Wednesday, December 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
calicinthias

Total Points: 447
Total Questions: 101
Total Answers: 118

Location: Botswana
Member since Sat, Dec 31, 2022
1 Year ago
calicinthias questions
Sun, Jan 2, 22, 00:00, 2 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Mon, Aug 10, 20, 00:00, 4 Years ago
;