Hosted web app - check connection before opening web app


#1

I am a beginner studying Ionic 2 and trying to create an app shell to my webapp, I was able to make it work using inappbrowser directly in my index.html like this:

    <script type="text/javascript" charset="utf-8">

// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
//
function onDeviceReady() {
     var ref = cordova.InAppBrowser.open('http://myWebApp.com', '_self', 'location=no, zoom=no, toolbar=no');
     ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); });
}

</script>

The problem is that now I am trying to check if there’s internet connection before I try to launch the web app but nothing happens with the code below, not even a error, just a blank screen after the splash screen…

<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// device APIs are available
//
function onDeviceReady() {
  if (navigator.connection.type == Connection.NONE){
    var ref = cordova.InAppBrowser.open('http://myWebApp.com', '_self', 'location=no, zoom=no, toolbar=no');
    ref.addEventListener('loaderror', function(event) { alert('error: ' + event.message); });
    ref.addEventListener('loadstop', function() {
    });
  } else {
    alert('you are offline');
  }
}
</script>

For now I don’t want to move my web app to run entirely native in the app, Im just learning and want to run it as a browser but making basic check of connectivity.

What am I missing? Is there a better way to do this? Is there any way to debug code which needs the device to run?
Remember I am a noob so explain your answer in more details if you can please :pray:


#2

I am a bit lost, what are you trying to accomplish?


#3

I just want to use inappbrowser to open a browser view with my website, I could do that but before opening the website I want to check if there is internet connection so if user is offline I can avoid the ‘page not found’ page in the app.


#4

HI,

Look at this http://ionicframework.com/docs/v2/native/network/

:wink: