How check if a device have connection when it's starting (and close app if not)

I’m using the native plugin network and in the main page i create an subscribe to manage the connection: Something like this

  ionViewDidEnter()
  {
    let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
      let vm = this;
      if(!vm.alertPresented) {
        vm.alertPresented = true;
        vm.alert.create({
          title: "Advertencia",
          subTitle: "Check your connection please.",
          buttons: [{
            text: 'OK',
            handler: () => {
              vm.alertPresented = false;
              this.navCtrl.push(OfflinePage);
            }
          }],
        }).present();
      }
    });
  }

How can i do a function when is loading the platform and exit the app if the user don’t have internet?
Thanks

Have you tried cordova-plugin-network-information.
I have another page, but it is in Angular 1. See if you can extract something.

For closing app, look at this question.
I think this.platform.exitApp() should work. But some say its not working for them.

yes i can close ionic with platform.exitapp(), but i don’t know how manage connections properly… I have an iframe and i want to avoid see crash the frame

Take a variable networkAvailable = false;.

<iframe [hidden]="!networkAvailable"></iframe>

Later in your JavaScript, detect network status using cordova-plugin-network-information and set the networkAvailable variabel to true.
This way user won’t see broken contents.

that’s a good one, i’ll implement and i tell you. Now i only need detect if a connection is too slow or if the user starts with no connection. Thanks

I don’t think that my previous suggestion is a solution for your asked question. Its just a partial solution for partial loaded content/iframe.

When you find a solution, just write down it here or give some reference(s) and mark that one as solution.

Yes, i need a lot of multiple parts of solutions haha

Yes but if i don’t have connection how will reload that iframe later?

Try using *ngIf if you want it to appear correctly with data whenever data gets available.

<iframe *ngIf="networkAvailable" [src]="http://some-url.com"></iframe>

OR -

Try holding the src value in a variable (in base64 format) and use it like this -

<iframe *ngIf="networkAvailable" [src]="dataVariable"></iframe>

This way you can check status of both networkAvailable and dataVariable before displaying <iframe> in HTML.
You would need to sanetize content of your dataVariable. Refer this.

You can access that property with network.type, it will give you wifi or none depending on the connection status, the events are used to track changes.

2 Likes

use ionic-native/network plugin
public network : Network;
if(network.type == ‘none’)
alert(‘You are offline’);