Search code examples
angularionic2cordova-plugins

How to use ionic 2 RC 4 native Network


I am following the ionic 2 docs on how to use Network which is a cordova plugin network information

 import { Network } from 'ionic-native';

 // watch network for a disconnect

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

// stop disconnect watch

    disconnectSubscription.unsubscribe();



  let connectSubscription = Network.onConnect().subscribe(() => {
       console.log('network connected!');


     setTimeout(() => {
       if (Network.type === 'wifi') {
        console.log('we got a wifi connection, woohoo!');
       }
     }, 3000);
   });

 // stop connect watch
 connectSubscription.unsubscribe();

How to use this in context of retrieving data from a server-side (e.g. when subscribing service.ts) so it keeps watching whether there is connection or not! can some provide a piece of code on how to utilize this! I'm not sure what is stored in disconnectSubscription so that I can set a condition statement to log the result (this is supposedly when I'm watching the network when retrieving data) and when should I unsubscribe the watch!

Also, there is an error on if (Network.type === 'wifi') property type does not exist of typeof Network


Solution

  • Make sure you are using ionic-native v2.2.12 or higher, there was a breaking change in this version on which they introduced Network.type.

    https://github.com/driftyco/ionic-native/releases/tag/v2.2.12