Whenever the app loads, my "network check" should run automatically instead of waiting for the user to click on the Check Connection
button. I tried putting it in a function but couldn't get it to work. Can anyone help me identify the syntax error in my if states == ...
statement?
Click to Check Connection Check Connection
import {Component} from '@angular/core';
import {NavController, Platform, AlertController} from 'ionic-angular';
declare var navigator: any;
declare var Connection: any;
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController, public platform: Platform, public alertCtrl: AlertController) { }
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';
// this. was missing
let alert = this.alertCtrl.create({
title: "Connection Status",
subTitle: states[networkState],
buttons: ["OK"]
});
// This is the proper way to present the alert
alert.present();
});
}
}
Prompt Displayed If No Connection
import {Component} from '@angular/core';
import {NavController, Platform, AlertController} from 'ionic-angular';
declare var navigator: any;
declare var Connection: any;
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController, public platform: Platform, public alertCtrl: AlertController) { }
ionViewDidLoad() {
this.platform.ready().then(() => {
var networkState = navigator.connection.type;
var states = {};
states[Connection.NONE] = 'No network connection';
if (states == states[Connection.NONE]) {
let alert = this.alertCtrl.create({
title: "Connection Status",
subTitle: states[networkState],
buttons: ["OK"]
});
alert.present();
}
});
}
}