I have installed the Cordova network plugin, but cannot seem to check network connection.
This is my code
import { Network } from 'ionic-native';
declare var navigator: any;
declare var Connection: any;
this.platform.ready().then(() => {
if(Network.connection === 'none') {
let alert = this.alertCtrl.create({
title: "Internet Connection",
subTitle:"Please Check Your Network connection",
buttons: [{
text: 'Ok',
handler: () => {
this.platform.exitApp();
}
}]
});
alert.present();
}
});
}
I get warning message
Native: deviceready did not fire within 2000ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them.
How could i go about resolving the issue?
Add this under platform ready. Once your app is up and running, try to disconnect and reconnect from\to internet. You should get the messages on your console.
let disconnectSub = Network.onDisconnect().subscribe(() => {
console.log('you are offline');
});
let connectSub = Network.onConnect().subscribe(()=> {
console.log('you are online');
});
I made use of the network import and ionic events to manage network disconnection and conection
checkDisconnection() {
const disconnectSubscription = this.network.onDisconnect().subscribe(() => {
this.checkConnection();
disconnectSubscription.unsubscribe();
this.nav.push('ErrorsPage');
});
}
checkConnection() {
const connectSubscription = this.network.onConnect().subscribe(() => {
this.checkDisconnection();
connectSubscription.unsubscribe();
this.nav.pop();
});
}
Works perfectly fine. Any more information you need please mention in comments
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With