Preview 2.0 is now in Public Beta!
Read the Announcement

Connectivity

The connectivity module provides a common abstraction of the functionality responsible for receiving information about the connection type and availability of the network.

Usage

import { Connectivity } from '@nativescript/core'

export function onNavigatedTo(args) {
  const page = args.object

  // Get the current connection type
  const type = Connectivity.getConnectionType()

  switch (type) {
    case Connectivity.connectionType.none:
      console.log('No connection')
      break
    case Connectivity.connectionType.wifi:
      console.log('WiFi connection')
      break
    case Connectivity.connectionType.vpn:
      console.log('VPN connection')
      break
    case Connectivity.connectionType.mobile:
      console.log('Mobile connection')
      break
    case Connectivity.connectionType.ethernet:
      console.log('Ethernet connection')
      break
    case Connectivity.connectionType.bluetooth:
      console.log('Bluetooth connection')
      break
    default:
      break
  }

  // Starts monitoring the network for changes
  Connectivity.startMonitoring(newConnectionType => {
    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.vpn:
        console.log('Connection type changed to VPN.')
        break
      case Connectivity.connectionType.mobile:
        console.log('Connection type changed to mobile.')
        break
      case Connectivity.connectionType.ethernet:
        console.log('Connection type changed to ethernet.')
        break
      case Connectivity.connectionType.bluetooth:
        console.log('Connection type changed to bluetooth.')
        break
      default:
        break
    }
  })

  // Stops monitoring the connection
  Connectivity.stopMonitoring()
}

Methods

NameTypeDescription
getConnectionTypenumberGets the type of connection. Returns a value from the connectivityModule.connectionType enumeration. To use this method on Android you need to have the android.permission.ACCESS_NETWORK_STATE permission added to the AndroidManifest.xml file.
startMonitoring(connectionTypeChangedCallback: function)voidStarts monitoring the connection type.
stopMonitoringvoidStops monitoring the connection type.

API References

NameType
@nativescript/core/connectivityModule
connectionTypeEnum

Native Component

AndroidiOS
CONNECTIVITY_SERVICE (android.content.Context)SCNetworkReachability