|
| 1 | +--- |
| 2 | +title: Connectivity |
| 3 | +--- |
| 4 | + |
| 5 | +## Connectivity |
| 6 | + |
| 7 | +The connectivity module provides a common abstraction of the functionality responsible for receiving information about the connection type and availability of the network. |
| 8 | + |
| 9 | +#### Usage |
| 10 | + |
| 11 | +```typescript |
| 12 | +import { Connectivity } from '@nativescript/core' |
| 13 | + |
| 14 | +export function onNavigatedTo(args) { |
| 15 | + const page = args.object |
| 16 | + |
| 17 | + // Get the current connection type |
| 18 | + const type = Connectivity.getConnectionType() |
| 19 | + |
| 20 | + switch (type) { |
| 21 | + case Connectivity.connectionType.none: |
| 22 | + console.log('No connection') |
| 23 | + break |
| 24 | + case Connectivity.connectionType.wifi: |
| 25 | + console.log('WiFi connection') |
| 26 | + break |
| 27 | + case Connectivity.connectionType.vpn: |
| 28 | + console.log('VPN connection') |
| 29 | + break |
| 30 | + case Connectivity.connectionType.mobile: |
| 31 | + console.log('Mobile connection') |
| 32 | + break |
| 33 | + case Connectivity.connectionType.ethernet: |
| 34 | + console.log('Ethernet connection') |
| 35 | + break |
| 36 | + case Connectivity.connectionType.bluetooth: |
| 37 | + console.log('Bluetooth connection') |
| 38 | + break |
| 39 | + default: |
| 40 | + break |
| 41 | + } |
| 42 | + |
| 43 | + // Starts monitoring the network for changes |
| 44 | + Connectivity.startMonitoring(newConnectionType => { |
| 45 | + switch (newConnectionType) { |
| 46 | + case Connectivity.connectionType.none: |
| 47 | + console.log('Connection type changed to none.') |
| 48 | + break |
| 49 | + case Connectivity.connectionType.wifi: |
| 50 | + console.log('Connection type changed to WiFi.') |
| 51 | + break |
| 52 | + case Connectivity.connectionType.vpn: |
| 53 | + console.log('Connection type changed to VPN.') |
| 54 | + break |
| 55 | + case Connectivity.connectionType.mobile: |
| 56 | + console.log('Connection type changed to mobile.') |
| 57 | + break |
| 58 | + case Connectivity.connectionType.ethernet: |
| 59 | + console.log('Connection type changed to ethernet.') |
| 60 | + break |
| 61 | + case Connectivity.connectionType.bluetooth: |
| 62 | + console.log('Connection type changed to bluetooth.') |
| 63 | + break |
| 64 | + default: |
| 65 | + break |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + // Stops monitoring the connection |
| 70 | + Connectivity.stopMonitoring() |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +#### Methods |
| 75 | + |
| 76 | +| Name | Type | Description | |
| 77 | +| ---------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 78 | +| `getConnectionType` | `number` | Gets 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. | |
| 79 | +| `startMonitoring(connectionTypeChangedCallback: function)` | `void` | Starts monitoring the connection type. | |
| 80 | +| `stopMonitoring` | `void` | Stops monitoring the connection type. | |
| 81 | + |
| 82 | +#### Connection Types |
| 83 | + |
| 84 | +- `none = 0`, |
| 85 | +- `wifi = 1`, |
| 86 | +- `mobile = 2`, |
| 87 | +- `ethernet = 3`, |
| 88 | +- `bluetooth = 4`, |
| 89 | +- `vpn = 5` |
| 90 | + |
| 91 | +#### API References |
| 92 | + |
| 93 | +| Name | Type | |
| 94 | +| --------------------------------------------------------------------------- | -------- | |
| 95 | +| [@nativescript/core/connectivity](/api-reference/modules.html#connectivity) | `Module` | |
| 96 | +| [connectionType](/api-reference/modules.html#connectivity) | `Enum` | |
| 97 | + |
| 98 | +#### Native Component |
| 99 | + |
| 100 | +| Android | iOS | |
| 101 | +| :---------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------- | |
| 102 | +| [CONNECTIVITY_SERVICE (android.content.Context)](https://developer.android.com/reference/android/content/Context) | [SCNetworkReachability](https://developer.apple.com/documentation/systemconfiguration/scnetworkreachability-g7d) | |
0 commit comments