Network

Improve this doc

$ ionic plugin add cordova-plugin-network-information

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

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

Supported platforms

Usage

import {Network, Connection} from 'ionic-native';

// watch network for a disconnect
let disconnectSubscription = Network.onDisconnect().subscribe(() => {
  console.log('network was disconnected :-( ')
});

// stop disconnect watch
disconnectSubscription.unsubscribe();


// watch network for a connection
let connectSubscription = 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(() => {
    console.log(Network.connection);
    if (Network.connection === Connection.WIFI) {
      console.log('we got a wifi connection, woohoo!');
    }
  });
});

// stop connect watch
connectSubscription.unsubscribe();

Static Methods

connection()

Return the network connection type

onDisconnect()

Watch the network for a disconnect (i.e. network goes offline)

Returns: Observable<any> Returns an observable.

onConnect()

Watch the network for a connection (i.e. network goes online)

Returns: Observable<any> Returns an observable.

API

Native

General