ROR
ROR

Reputation: 271

check for internet connection in PhoneGap for all platform?

this is for two days I am searching for how to detect the internet connection in phonegap using my chrome browser, then I checked many answers on Stack and google but unfortunatly I couldn't find any perfect answer, now I want to ask directly here how can I check the internet connection in phongegap using dreamwaever with windows 8 , is there any way to do this, in addition I want to test on chrome browser then convert it to apps on Adobe eng. Thanks for any help.

I faced these errors without any alert:

enter image description here

Upvotes: 2

Views: 465

Answers (1)

Amr Bahaa
Amr Bahaa

Reputation: 1157

You can use cordova plugin :

<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>

then for checking the connection:

function checkConnection() {
  var objConnection = navigator.network.connection;
  if (objConnection.type == Connection.NONE) {
    //Do somthing 
  }

}

for more details about the cordova network plugin and the connection types : cordova-plugin-network

Hope this help.

Update

lets say that you want to check the connection on device ready event:

Register checkconnection function to deviceready event.

document.addEventListener("deviceready", checkConnection, false);

check the connection if there is no connection for example :show him a message or do whatever action you want :

function checkConnection() {
      var objConnection = navigator.network.connection;
      if (objConnection.type == Connection.NONE) {
        alert("Please check your network connection"); 
      }

    }

Note: You can replace or use any other phone gap event to check the connection like "offline" event. So if you regiter the checkconnction function to offline event like this:

document.addEventListener("offline", checkConnection, false);

the alert message will be shown to user if he lost the connection.

for more info about Cordova lifecycle events please check cordova events

Upvotes: 2

Related Questions