facebook

PhoneGap Check internet connection: CLOSED

💡
Our Forums Have Moved

For help with installation, bugs reports or feature requests, please head over to our new forums.
Genuitec Community on GitHub

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #328136 Reply

    Yann
    Member

    Hello
    i try to detect if phone is connected to internet
    i try this script from phonegap but he don’t work

    function checkConnection()
       {       
        var networkState = navigator.network.connection.type;        
        var states = {};        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = '1';        
        states[Connection.WIFI]     = '1';        
        states[Connection.CELL_2G]  = '1';        
        states[Connection.CELL_3G]  = '1';        
        states[Connection.CELL_4G]  = '1';        
        states[Connection.NONE]     = '0';
        if (states[networkState]=='0')
        {
        alert(No connection.');
        }
    
    phoneui.documentReadyHandler = function() {
      
      checkConnection();
    }
    }

    can you help me
    of course i generated an ipa file to test on my phone.
    thanks
    Yann

    #328139

    Kale
    Member

    Hi,

    the phoneui.documentReadyHandler = function() is inside of your checkConnection() function. So it is never called.

    Try:

    phoneui.documentReadyHandler = function() {

    checkConnection();
    }

    function checkConnection()
    {
    var networkState = navigator.network.connection.type;
    var states = {}; states[Connection.UNKNOWN] = ‘Unknown connection’;
    states[Connection.ETHERNET] = ‘1’;
    states[Connection.WIFI] = ‘1’;
    states[Connection.CELL_2G] = ‘1’;
    states[Connection.CELL_3G] = ‘1’;
    states[Connection.CELL_4G] = ‘1’;
    states[Connection.NONE] = ‘0’;
    if (states[networkState]==’0′)
    {
    alert(No connection.’);
    }
    }

    Didn´t check, hope it works.

    Kale

    #328140

    Yann
    Member

    Hello Kale
    thanks for your answer but it’s an error of copy/paste
    in my script my function is outside phoneui.documentReadyHandler = function
    Thanks
    Yann

    #328146

    Kale
    Member

    Hi,

    the problem could be, that a phonegap function is called, before the phonegap framework is fully loaded.

    Try this:

    phoneui.documentReadyHandler = function() {
    document.addEventListener(“deviceready”, onDeviceReady, false);
    }
    function onDeviceReady() {
    checkConnection();
    }

    function checkConnection(){
    var networkState = navigator.network.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’;

    if (states[Connection.NONE]){
    alert(‘No Connection!’);
    }

    alert(states[networkState]);
    }

    Should work now.

    Kale

    #328166

    Yann
    Member

    Hello Kale
    thanks again, now i’ve an alert with “no connection” even though I am connected 🙁
    so i test another way by testing a response.statut with xmlHttpRequest with success but i would like use

    
    if (req.readyState == 4) {
           // only if "OK"
             if (req.status == 200 || req.status == 304) {
               results = req.responseText;
               document.getElementById(target).innerHTML = results;
           } else {
            
                         noInternet();
           }
    
    function noInternet() 
       {    
           navigator.notification.vibrate(2500);
           navigator.notification.confirm(
                'Aucune connexion internet trouvée.',  // message
                onConfirm,              // callback to invoke with index of button pressed
                'Erreur',            // title
                'Close'          // buttonLabels
            ); 
    

    but nothing happen, an idea?
    thanks
    Yann

    #328170

    Yann
    Member

    i found my problem
    thanks for your help Kale
    Yann

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: PhoneGap Check internet connection: CLOSED

You must be logged in to post in the forum log in