Connectivity

Obtaining connectivity information requires the "connectivity" module.

import * as connectivity from "tns-core-modules/connectivity";

Getting connection type

var connectionType = connectivity.getConnectionType();
switch (connectionType) {
    case connectivity.connectionType.none:
        //console.log("No connection");
        break;
    case connectivity.connectionType.wifi:
        //console.log("WiFi connection");
        break;
    case connectivity.connectionType.mobile:
        //console.log("Mobile connection");
        break;
}

Monitoring connection type.

connectivity.startMonitoring(function onConnectionTypeChanged(newConnectionType: number) {
    switch (newConnectionType) {
        case connectivity.connectionType.none:
            //console.log("Connection type changed to none.");
            break;
        case connectivity.connectionType.wifi:
            //console.log("Connection type changed to WiFi.");
            break;
        case connectivity.connectionType.mobile:
            //console.log("Connection type changed to mobile.");
            break;
    }
});
//...
connectivity.stopMonitoring();

Permissions

On Android, the permission ACCESS_NETWORK_STATE is required.

Stay connected with NativeScript

(expect a newsletter every 4-8 weeks)

NativeScript
NativeScript is licensed under the Apache 2.0 license .
© Progress Software Corporation. All Rights Reserved.