Wednesday, 10 July 2013

How to detect the internet connection in Sencha Touch 2

Today we are going to see how to detect the mobile device is connected to internet or not in Sencha Touch 2. We are going to use Ext.device.Connection class that contains isOnline() method, which is used to check if the current device is currently online or not.  This method will return true, if you are currently online otherwise return false.


Let's see the implementation
if (Ext.device.Connection.isOnline()) {

    Ext.Msg.alert("I am online");
} else {

    Ext.Msg.alert("I am offline");
}

Here is the output, when i run the above code in google chrome with internet connection


Here is the output, when i run the above code in google chrome without internet connection


Important Note:

You need to add the Ext.device.Connection class as the requires in the Ext.Application class, otherwise you will get connection is undefined error.

Hope, you enjoyed this Post.

4 comments:

  1. Hi,

    How do you define Ext.device.Connection in Ext.Application?

    I tried:

    Ext.application({
    appFolder: 'XXX',
    name: 'XXX',
    requires: 'Ext.device.Connection',
    });

    But it can't find Connection.js

    Any help would be greatly appreciated.

    ReplyDelete
    Replies
    1. I think Sencha Touch can't able to load SDK files. Please make sure, SDK path is set correctly.

      Thanks and Regards,
      Suresh Ariya

      Delete
  2. Please add Conection.js file on App.js's requires array

    ReplyDelete
  3. this is not working in android build...
    i gave permission int androidmanifest.xml , but its invain

    ReplyDelete