Need help upgrading to Ionic Framework 4.0? Get assistance with our Enterprise Migration Services EXPLORE NOW

Network

Improve this doc

Requires Cordova plugin: cordova-plugin-network-information. For more info, please see the Network plugin docs.

Repo: https://github.com/apache/cordova-plugin-network-information

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add cordova-plugin-network-information
    $ npm install --save @ionic-native/network@4
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { Network } from '@ionic-native/network';

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();

Instance Members

Connection

Constants for possible connection types

type

Connection type

Returns: string

downlinkMax

Downlink Max Speed

Returns: string

onchange()

Returns an observable to watch connection changes

Returns: Observable<any>

onDisconnect()

Get notified when the device goes offline

Returns: Observable<any> Returns an observable.

onConnect()

Get notified when the device goes online

Returns: Observable<any> Returns an observable.

Advanced

The type property will return one of the following connection types: unknown, ethernet, wifi, 2g, 3g, 4g, cellular, none

API

Native

General