Network
Requires Cordova plugin: cordova-plugin-network-information. For more info, please see the Network plugin docs.
https://github.com/apache/cordova-plugin-network-information
Stuck on a Cordova issue?
If you're building a serious project, you can't afford to spend hours troubleshooting. Ionic’s experts offer premium advisory services for both community plugins and premier plugins.
Installation
- Capacitor
- Cordova
- Enterprise
$ npm install cordova-plugin-network-information
$ npm install @awesome-cordova-plugins/network
$ ionic cap sync
$ ionic cordova plugin add cordova-plugin-network-information
$ npm install @awesome-cordova-plugins/network
Ionic Enterprise comes with fully supported and maintained plugins from the Ionic Team. Learn More or if you're interested in an enterprise version of this plugin Contact Us
Supported Platforms
- Amazon Fire OS
- Android
- Browser
- iOS
- Windows
Usage
React
Learn more about using Ionic Native components in React
Angular
import { Network } from '@awesome-cordova-plugins/network/ngx';
constructor(private network: Network) { }
...
// watch network for a disconnection
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
});
// stop disconnect watch
disconnectSubscription.unsubscribe();
// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() => {
console.log('network connected!');
// We just got a connection but we need to wait briefly
// before we determine the connection type. Might need to wait.
// prior to doing any api requests as well.
setTimeout(() => {
if (this.network.type === 'wifi') {
console.log('we got a wifi connection, woohoo!');
}
}, 3000);
});
// stop connect watch
connectSubscription.unsubscribe();