Phonegap check internet connection
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);
Admin
Updated on June 04, 2022Comments
-
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 almost 11 yearsWhich platform are you testing this on?
-
Amit Prajapati almost 11 yearsdid you add network connection plugin ?
-
Admin almost 11 yearsYes i added the plugin in config.xml because in phonegap build the plugins.xml files doesn't exists
-
-
Admin almost 11 yearsI tried this too, but the event fire only if i lose the connection when i use the app.
-
Admin almost 11 yearsIt gives me the next error: "connection" doesn't exists for undefined..which means it doesn't recognize the method network for navigatoe
-
Admin almost 11 yearsWhat this mean?.. i mean i didn't use any of iframe html tag :-??
-
Admin almost 11 yearsyes, I tried, the same error: "Uncaught TypeError: Cannot read property of undefined at file:///android_asset/www/index.html:29"
-
SHANK almost 11 yearsAdded another permission to access the internet state. Check now
-
Admin almost 11 yearsi 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 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 almost 11 yearsYes, is what i'm expecting but it doesn't works. That events don't fire.
-
ULLAS MOHAN.V almost 11 yearscheck 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 almost 11 yearsIt says that Connection is not defined and the entire aplication failed. It's about Connection.NONE.
-
ULLAS MOHAN.V almost 11 yearsAm confident that it will works.. cause we are using this in our current project
-
ULLAS MOHAN.V almost 11 yearswhich cordova version is u are using?
-
Admin almost 11 yearsI'm using Cordova-3.0.0
-
ULLAS MOHAN.V almost 11 yearsTry this. For instance, your "deviceready" callback: document.addEventListener("offline", function() { alert("No internet connection"); }, false);
-
Piyush almost 11 yearsI think in your that page phonegap not loaded So first check phonegap loaded or not in your app?
-
Dibish over 10 yearsThank you so much for your help, its helped me to solve my connection check issue...
-
Shafi PS over 10 years@ Piyush, i need to work this code when the internet connection is blocked while running my app.
-
Piyush over 10 years@Shafips please specify your problem or post new question of your problem so i can help you