I need help with ionic Network plugin. Here's a code, but it doesn't work for me. No console log or any other modals. No errors. at the top
import { Network } from '@ionic-native/network';
ionViewDidLoad() {
this.getWarrentsNumber();
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
});
disconnectSubscription.unsubscribe();
let connectSubscription = this.network.onConnect().subscribe(() => {
console.log('network connected!');
setTimeout(() => {
if (this.network.type === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
connectSubscription.unsubscribe();
}
Declare your variables
connectSubscription
&disconnectSubscription
as class properties and then try tounsubscribe()
them inioniViewWillLeave()
hook instead ofionViewDidLoad()
. So the code would look similar to the following -Here is the quote from plugin's official github repo
As you can see onConnect will only emit something when previously unconnected device receives a network connection.
To check on startup if device is online you can directly check
this.network.type
Or
You can create a service which will handle all these
And then you can inject your service where-ever you want and subscribe to connection.
That code works properly but it works dependent platform like android or ios. It may not console anything on the browser I think. Please test your application with respect to the platform device.
or else you can use ngOnInit() instead of ionViewDidLoad();