Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
NikhilPuri
Explorer
This blog post is around a key feature which is used in Hybrid Mobile Application. The applications which can be used both online & offline. So basically developer add a code which detects if the application is connected to internet or not, depending to it features of the app are enabled or disabled.

 

What is the issue ?


So, In Mobile apps you too would have used the network state to toggle offline/online mode. Its usually done by Cordova network plugins. So, whats the issue with standard method , why is the method I am discussing better. The standard plugin uses an older version of Cordova network plugins. The standard method had issues, Cordova fixed those in version 2.3.0 which is not available for SAP yet . Irrespective of the connection state it always return “UNKNOWN”.

 
function checkNetwork(){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';alert('Connection type: ' + states[networkState]);}checkNetwork();

What’s a better yet simple a way to do it ? 

You can determine that the connection is lost by making failed XHR requests.

Sidenote: You could check a reliable site like google for connectivity, but this may not be entirely useful as just trying to make your own request, because while Google may be available, your own application may not be, and you're still going to have to handle your own connection problem. Trying to send a ping to google would be a good way to confirm that the internet connection itself is down, so if that information is useful to you, then it might be worth the trouble.
$.ajax({type: "GET",url: "https://www.google.com/",success: function (data,resp) {console.log(resp);console.log('Network connection is working');},error: function (data,resp) {console.log(data.statusText);console.log('Network connection is  not working');}});

Conclusion


It’s a simple example of checking if device is online or not. Hope it will help those who are facing this issue & add some knowledge to those who haven’t worked on such application.

Labels in this area