Checking network connection


#1
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

    .run(function($ionicPlatform) {
    	
      $ionicPlatform.ready(function() {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
          // org.apache.cordova.statusbar required
          StatusBar.styleLightContent();
        }
        if(window.Connection) {
          if(navigator.connection.type == Connection.NONE) {
              $ionicPopup.confirm({
                  title: "Internet Disconnected",
                  content: "The internet is disconnected on your device."
                })
              .then(function(result) {
                  if(!result) {
                  ionic.Platform.exitApp();
                  }
                });
              }
           }
    }) 

I’m trying to add a check network connection to my app which checks to see if the user has available internet connection while opening the application but isn’t working. Any idea why?


#2

Check these two useful tutorials on ionic
http://www.codeandyou.com/2015/07/how-to-check-network-connection-in.html


#3

you have to add the cordova-plugin-network-information plugin to use this.

Than keep in mind… you can have wifi acccess but no internet access then the ConectionType is not NONE!

And i your device goes offline/online during your app is open you need to listen to the online, offline events triggered by the network-information plugin:

        document.addEventListener('online', function () {
           // is online
        }, false);
        document.addEventListener('offline', function () {
            // is offline
        }, false);

#4

I actually wrote a step by step tutorial on how to create an application that uses this plugin, and you can take a look at it here: http://www.nikola-breznjak.com/blog/codeproject/check-network-information-change-with-ionic-famework/.

Please let me know if it will prove to be helpful to you.