Ionic 2 Cordova network information plugin reference error - help please?

I’ve installed the network information plugin as per this tutorial into my Ionic 2 app:

Ionic 2 | How to Work With Cordova Plugins

However TypeScript won’t compile because it can’t find the reference for “Connection” in the states array lines. Any idea how to write the import statement for it as per Platform, Page etc?

My class:

import {NavController, NavParams} from 'ionic-framework/ionic';
import {Page, ViewController, Platform, Alert, Modal, Events} from 'ionic-framework/ionic';
import {forwardRef} from 'angular2/core';
import {OnInit} from 'angular2/core';
import {Injectable} from 'angular2/core';
import {TodosService} from '../../services/TodosService';
import {MyTodosItem} from '../../pages/my-todos-item/my-todos-item';

@Page({
  templateUrl: 'build/pages/my-todos/my-todos.html'
})
class TodayPage {

  constructor(
    private platform: Platform,
    private nav: NavController,
    private _events: Events,
    private _todosService: TodosService) {
    this._platform = platform;
    this._isAndroid = platform.is('android');
  }

  ngOnInit() {
    this.getItems();
    this._events.subscribe('item:updated', () => {
      this.getItems();
    });

    this.checkNetwork();
  }

  checkNetwork() {
      this._platform.ready().then(() => {
          var networkState = navigator.connection.type;

          var states = {};
          states[Connection.UNKNOWN]  = 'Unknown connection';
          states[Connection.ETHERNET] = 'Ethernet connection';
          states[Connection.WIFI]     = 'WiFi connection';
          states[Connection.CELL_2G]  = 'Cell 2G connection';
          states[Connection.CELL_3G]  = 'Cell 3G connection';
          states[Connection.CELL_4G]  = 'Cell 4G connection';
          states[Connection.CELL]     = 'Cell generic connection';
          states[Connection.NONE]     = 'No network connection';

          alert(states[networkState]);
      });
  }
.. 
}

Error is:
Cannot find name ‘Connection’.

Redefine states with the following code:

states.UNKNOWN  = 'Unknown connection';
states.ETHERNET = 'Ethernet connection';
states.WIFI     = 'WiFi connection';
states.CELL_2G  = 'Cell 2G connection';
states.CELL_3G  = 'Cell 3G connection';
states.CELL_4G  = 'Cell 4G connection';
states.CELL     = 'Cell generic connection';
states.NONE     = 'No network connection';

Connection is not defined, the example from the github repo is wrong IMO.

Thanks for your answer, but that doesn’t actually solve the problem, it merely moves it into a magical corner as Connection (and any further plugin Types I would need) is still undefined!

For anyone who has this issue, I’ve fixed it now using the phonegap.d.ts definitions, see my answer to my question on StackOverflow here:

http://stackoverflow.com/questions/35652301/ionic-2-cordova-network-information-plugin-reference-error

2 Likes