Phonegap check internet connection

12,657

Solution 1

You can check the connection like this:

function CheckConnection()
{
     if( !navigator.network )
     {
         // set the parent windows navigator network object to the child window
         navigator.network = window.top.navigator.network;
     }

    // return the type of connection found
   return ( (navigator.network.connection.type === "none" || navigator.network.connection.type === null || 
          navigator.network.connection.type === "unknown" ) ? false : true );
}

returns true for connection and false for no connectivity.

In Android Manifest, use the following permission:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Edit: Handles windows in iframes as well. Added another permission.

Solution 2

Hello Zvîncă Alin Ionuț,

Following Code should be work in phonegap for internet check...

var networkState = navigator.connection.type;
    if (networkState == Connection.NONE){
         alert('No Internet');      
    }else{
         alert('Internet Connection there');
    }

Application also needs permision to connect internet so add above line in config.xml.

<feature name="http://api.phonegap.com/1.0/network"/>

Solution 3

Try This I wiil be work in phonegap for internet check.

//Check Internaet Connection.................................

  //If User is Online.................................

function onOnline() {               
            alert("Internet connected")
}

//If User is Offline....................................
document.addEventListener("offline", onOffline, false);
function onOffline() {
    alert("Internet not connected")

}
    document.addEventListener("online", onOnline, false);
Share:
12,657
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    How can i check the internet connection in cordova 3.0 build? Because i tried

    function ondeviceready(){  checkconnection(); alert(navigator.connection.type); }
    

    but it returns always 0.

    I heard about jquery mobile but i don't know how to use it because i tried

     function checkconnection(){   alert(navigator.onLine);}
    

    with jquery mobile library loaded but it returns always true.

    This is happening on my smartphone and on AVD too.

    Can someone help me?

    • Raoul George
      Raoul George almost 11 years
      Which platform are you testing this on?
    • Amit Prajapati
      Amit Prajapati almost 11 years
      did you add network connection plugin ?
    • Admin
      Admin almost 11 years
      Yes i added the plugin in config.xml because in phonegap build the plugins.xml files doesn't exists
  • Admin
    Admin almost 11 years
    I tried this too, but the event fire only if i lose the connection when i use the app.
  • Admin
    Admin almost 11 years
    It gives me the next error: "connection" doesn't exists for undefined..which means it doesn't recognize the method network for navigatoe
  • Admin
    Admin almost 11 years
    What this mean?.. i mean i didn't use any of iframe html tag :-??
  • Admin
    Admin almost 11 years
    yes, I tried, the same error: "Uncaught TypeError: Cannot read property of undefined at file:///android_asset/www/index.html:29"
  • SHANK
    SHANK almost 11 years
    Added another permission to access the internet state. Check now
  • Admin
    Admin almost 11 years
    i have all this three permissions added : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> and is not working
  • ULLAS MOHAN.V
    ULLAS MOHAN.V almost 11 years
    @Zvîncă Alin Ionuț : Yes.. In this its works only if u loose connection . That means when u use app ; first it checks our device is online or not. Then what u expect ?
  • Admin
    Admin almost 11 years
    Yes, is what i'm expecting but it doesn't works. That events don't fire.
  • ULLAS MOHAN.V
    ULLAS MOHAN.V almost 11 years
    check my code now and try once again ! Hope u add cordova.js in ur html code. try to add your script file like <script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script> <script type="text/javascript" src="script/jquery.js"></script> <script type="text/javascript" src="script/jqm.js"></script> <script type="text/javascript" src="script/jquery.mobile.iscrollview.js"></script> <script type="text/javascript" src="script/app.js"></script>
  • Admin
    Admin almost 11 years
    It says that Connection is not defined and the entire aplication failed. It's about Connection.NONE.
  • ULLAS MOHAN.V
    ULLAS MOHAN.V almost 11 years
    Am confident that it will works.. cause we are using this in our current project
  • ULLAS MOHAN.V
    ULLAS MOHAN.V almost 11 years
    which cordova version is u are using?
  • Admin
    Admin almost 11 years
    I'm using Cordova-3.0.0
  • ULLAS MOHAN.V
    ULLAS MOHAN.V almost 11 years
    Try this. For instance, your "deviceready" callback: document.addEventListener("offline", function() { alert("No internet connection"); }, false);
  • Piyush
    Piyush almost 11 years
    I think in your that page phonegap not loaded So first check phonegap loaded or not in your app?
  • Dibish
    Dibish over 10 years
    Thank you so much for your help, its helped me to solve my connection check issue...
  • Shafi PS
    Shafi PS over 10 years
    @ Piyush, i need to work this code when the internet connection is blocked while running my app.
  • Piyush
    Piyush over 10 years
    @Shafips please specify your problem or post new question of your problem so i can help you