Ionic native network plugin


#1

I need to check internet and id connection exist I need to perform a server request. I used ionic native network Plugin. But i done as per their official doc.

my CODE:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic-angular';
import { Auth } from '../../providers/auth/auth';
import { Network } from '@ionic-native/network';
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
  constructor(private network: Network,private alertCtrl: AlertController,public navCtrl: NavController, public navParams: NavParams,public authService: Auth, public loadingCtrl: LoadingController) {
  
}
  ionViewDidLoad() {
      // watch network for a disconnect
    let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
        console.log('network was disconnected :-(');
    });
  disconnectSubscription.unsubscribe();
  // watch network for a connection
  let connectSubscription = this.network.onConnect().subscribe(() => {
    console.log('network connected!');
    //Check if already authenticated
    this.authService.checkAuthentication().then((res) => {
        console.log("Already authorized");
        this.loading.dismiss();
        this.navCtrl.setRoot(HomePage);
    }, (err) => {
        console.log("Not already authorized");
        this.loading.dismiss();
    });
  });
  
  // stop connect watch
  connectSubscription.unsubscribe();
    console.log('ionViewDidLoad LoginPage');
  }
}

Here it does not even entering into onConnect!! Why does this happen?


#2

If this is the first page of your app the Cordova plugins might not yet be ready. Try to wrap your first call with network inside this block:

this.platform.ready().then(() => {
  // Do your network stuff now
});

Of course make sure you add the platform to your constructor as well (like in app.component.ts)!


#3

Error shows in this as:

Unexpected token. A constructor, method, accessor, or property was expected.