I am building a Cordova app where-in i am using Cordova network-information plugin org.apache.cordovawork-information. From past few days i am getting this strange bug in web-inspector when debugged in safari where in it says "typeerror 'undefined' is not an object (evaluating 'navigator.connection.type')". During initial load it is working fine but on navigating further with in the app this error is being displayed and app freezes. In iOS 8 it occurs frequently but in Android lollipop it occurs occasionally. I have tried all the suggestions or posts regarding this but it leads me nowhere. I am using Cordova version 4.3, iOS 8.1. Any help is much appreciated Sorry couldn't post any images regarding this as i haven't got enough reputation to post it
function checkConnection() {
alert(navigator.connection.type);
var networkState = navigator.connection.type;
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';
if(networkState == Connection.UNKNOWN || networkState == Connection.NONE){
navigator.notification.alert('No Network Available',null,"Warning");
sessionStorage.setItem('UserID',"");
$.mobile.changePage("#loginPage", {
transition: 'none',
showLoadMsg: true
});
return false;
}else{
return true;
}
}
I am building a Cordova app where-in i am using Cordova network-information plugin org.apache.cordovawork-information. From past few days i am getting this strange bug in web-inspector when debugged in safari where in it says "typeerror 'undefined' is not an object (evaluating 'navigator.connection.type')". During initial load it is working fine but on navigating further with in the app this error is being displayed and app freezes. In iOS 8 it occurs frequently but in Android lollipop it occurs occasionally. I have tried all the suggestions or posts regarding this but it leads me nowhere. I am using Cordova version 4.3, iOS 8.1. Any help is much appreciated Sorry couldn't post any images regarding this as i haven't got enough reputation to post it
function checkConnection() {
alert(navigator.connection.type);
var networkState = navigator.connection.type;
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';
if(networkState == Connection.UNKNOWN || networkState == Connection.NONE){
navigator.notification.alert('No Network Available',null,"Warning");
sessionStorage.setItem('UserID',"");
$.mobile.changePage("#loginPage", {
transition: 'none',
showLoadMsg: true
});
return false;
}else{
return true;
}
}
Share
Improve this question
edited Apr 6, 2015 at 7:29
arun_K
asked Apr 6, 2015 at 7:24
arun_Karun_K
3531 gold badge4 silver badges13 bronze badges
6
- Try having a look at this stackoverflow./questions/26790729/… – Jonathan Smith Commented Apr 6, 2015 at 7:32
-
Also, does anything get logged to
platforms/ios/cordova/console.log
? – Jonathan Smith Commented Apr 6, 2015 at 7:39 - I have just tried the link but it lead me nowhere as well :-( – arun_K Commented Apr 6, 2015 at 7:51
- @jonnyknowsbest sorry i couldn't find a console.log file in there – arun_K Commented Apr 6, 2015 at 7:54
- 1 Have you added cordova.js in your file? Also you have to checkConnection in onDeviceReady – Uttam Sinha Commented Apr 6, 2015 at 8:51
3 Answers
Reset to default 2In my case, the problem was that I was calling the function out of the device ready
So doing this fixed my problem:
document.addEventListener("deviceready", function(){
// do your navigator.connection.type stuff here
}, false);
Check whether you have declared these
<feature name="NetworkStatus">
<param name="android-package" value="CDVConnection" />
</feature>
Network status will be the feature name and value will be your class name.
In Android Manifest,
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Aso check cordova_plugin.js
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [{
"file": "plugins/org.apache.cordova.dialogs/www/notification.js",
"id": "org.apache.cordova.dialogs.notification",
"merges": ["navigator.notification"]
}, {
"file": "plugins/org.apache.cordovawork-information/www/network.js",
"id": "org.apache.cordovawork-informationwork",
"clobbers": ["navigator.connection", "navigatorwork.connection"]
}];
module.exports.metadata = // TOP OF METADATA
{
"org.apache.cordova.device": "0.2.8",
"org.apache.cordovawork-information": "0.2.7"
}
});
I had the same issue using cordova-plugin-network-information. What I noticed was that despite being called after "deviceready" is fired, the navigator.connection.type object is still undefined. My workaround was to delay the call by 200ms, after which the object was available for use.
function checkConnection(){
try{
console.log(navigator.connection.type);
}
catch(e){
alert("error : "+e);
}
setTimeout(function(){
var networkState = navigator.connection.type;
console.log(networkState);
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]);
if(states[networkState] == "No network connection"){
window.location.assign("lostConnection.html");
}
}, 200);
}
So the "try/catch" sequence will fail with an undefined object, but 200ms later the call to navigator.connection.type is successful, well for me anyway!