Resolving network connectivity issues with PhoneGap 3.3.0

January 13, 2014, 10:11 pm
Author: James Griffiths

To say we were baffled would be an understatement. Our code had been working perfectly in the previous PhoneGap version and, after checking the online docs, there hadn't been any functions deprecated that we were using in the upgraded PhoneGap version.

What was going on?! More importantly - how were we going to fix this?

After some goggling and tinkering with the code we managed to find a solution to what appears to be a bug with PhoneGap version 3.3.0 itself. It seems that the network connectivity check using the navigator object is inaccessible on application load UNLESS it is wrapped inside a javascript SetTimeout call.

   function checkConnection(){ 
   var networkState = navigator.connection.type; 
   setTimeout(function() { 
      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.NONE] = 'No network connection'; 

      Rest of the function continued here.... 
   }, 750); 

Once we added this 'hack' (because that's essentially what it is) we found we could run the App exactly as it behaved on the previous PhoneGap build.

Always nice to find a solution to coding headaches but frustrating that this appears to be an issue inherent to the current version of PhoneGap.


« Return to Posts

Post a comment

All comments are welcome and the rules are simple - be nice and do NOT engage in trolling, spamming, abusiveness or illegal behaviour. If you fail to observe these rules you will be permanently banned from being able to comment.