headkit
headkit

Reputation: 3327

Determine if internet connection is available

how do you folks check if the device is connected to the internet in a sencha touch app?

Upvotes: 2

Views: 5438

Answers (2)

mattwindwer
mattwindwer

Reputation: 929

If there is no network connection during an ajax request sencha touch will return a response with a 0 status. So this is what we're using in our app (works in phonegap too):

// global error handling
Ext.Ajax.on('requestexception', function(connection, response) {
  // get rid of any loading masks that are present
  Ext.each(Ext.query('.x-mask'), function(el) {
      new Ext.Element(el).hide()
  });
  switch(response.status) {
    case 0 :
      Ext.Msg.alert('Error Loading', 'No Internet connection.');
      break;
    case 401 :
      Ext.Msg.alert('Login Failed', 'Check your username and password.');
      break;
    default :
      Ext.Msg.alert('Whoops!', 'An unexpected error has occurred and we have been alerted.');
  }
});

Note that this is for touch 1.1, haven't tried in in 2.0 yet.

Upvotes: 2

Jacob Oscarson
Jacob Oscarson

Reputation: 6393

There's an attribute called navigator.onLine (general browser support, not specific for Sencha)

If I'm in a PhoneGap application (which you often are if you're using Sencha Touch), I'd rather use their network.isReachable function, since I've by experience found it more reliable.

There's also something called 'Offline events', John Resig describes them on his blog: http://ejohn.org/blog/offline-events/.

Upvotes: 3

Related Questions