0

i am developing a conference app that is data driven and constantly will be updated from the web server. i am storing data on the local storage for persistence, but when the app is installed and launched for the first time i want to pop up a "No Internet Connection" message and close the app when they click any button on the pop up. but when there is internet load the app. i have done this in my app.run function but it does not work.

var app = angular.module('starter', ['ionic', 'ionic-material', 'ngCordova']);

app.run(function ($ionicPlatform, $ionicPopup, $timeout) {
    $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.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        //checking for internet connection on startup
        if (window.Connection) {
            if (navigator.connection.type === Connection.NONE) {
                document.addEventListener("offline", function () {
                    $ionicPopup.confirm({
                        title: "Internet Disconected",
                        content: "Sorry, No internet connection now, please try again"
                    }).then(function (result) {
                        if (!result) {
                            $ionicPlatform.exitApp();
                        }
                    });
                }, false);
            }
        }
    });
});

the app pops up the message, but on click of any of the buttons(ok and cancel), the app just stays on white screen. it does not exit the app. i don't know what am doing wrong. please i need advice and code samples to correct my error.

Jatto_abdul
  • 109
  • 2
  • 11

2 Answers2

2

A few aspects to keep in mind:

  • your implementation of exitApp() is reported not to work in iOS devices

  • kill an app is a big no for usability, you'd better present the interface with the latest chached data or if any data is cached a "no network connection" message integrated into the app layout (checkout Spotify for an example)

In any case, your purpose could be reached with ngCordova.plugins.network module, bundled in http://ngcordova.com/

This an example of service that returns current network status:

angular.module('app.common.connectivity', [
  'ngCordova.plugins.network'
])

.service('ConnectivityService', function($cordovaNetwork) {
  this.isOnline = $cordovaNetwork.isOnline() || false;
});

You can add this module and inject the service where needed, like in:

var app = angular.module('starter', ['ionic', 'ionic-material', 'app.common.connectivity']);

app.run(function ($ionicPlatform, $ionicPopup, $timeout, ConnectivityService) {
    $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.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

        //checking for internet connection on startup
        if( !ConnectivityService.isOnline && !window.localStorage.appLaunched ) {
          $ionicPopup.confirm({
            title: "Internet Disconected",
            content: "Sorry, No internet connection now, please try again"
          })
          .then(function(result) {
            $ionicPlatform.exitApp();
          });
        }

        // Apparently we're online so remember we already have been here
        if ( !localStorage.appLaunched ) localStorage.appLaunched = true;
    });
});
Enrico Deleo
  • 388
  • 2
  • 15
  • thanks Enrico, i am trying to check for connection when the user loads the app for the first time. i am already loading data from the local storage in each of my controllers (bad practice though), but i need to check for internet connection and exit the app if the user has never opened the app with internet connection before. and if he has loaded with internet connection once... i need it to bypass that function and move to my controllers logic which will load the data from the local storage. u can check my codes [here](https://github.com/jattoabdul/hiveAfrica). – Jatto_abdul Oct 12 '16 at 12:13
  • 1
    Just fixed an error on my snippet (I hadn't negate ConnectivityService.isOnline, now it is). You can persist a variable in localstorage the first time the user is online, and use that value to bypass forced exit. I'm going to update the answer. – Enrico Deleo Oct 12 '16 at 13:49
0

Reading their doc it say's to use

ionic.Platform.exitApp();

To exit a app.

Also have you verified your code even makes it to that condition.

Darkrum
  • 1,325
  • 1
  • 10
  • 27