Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-native-network-info

pusherman65.4kMIT5.2.1TypeScript support: included

Get local network information

react-component, react-native, ios, network, lan

readme

react-native-network-info

React Native library for getting information about the devices network

Requirements

Version 3+ requires RN 0.47 or higher Version 2+ requires RN 0.40 - RN 0.46

Installation

npm install react-native-network-info --save

or

yarn add react-native-network-info

Linking the library

Using React Native >= 0.60

Linking the package manually is not required anymore with Autolinking.

iOS also requires CocoaPods install

$ cd ios && pod install && cd ..

Using React Native >= 0.60

react-native link react-native-network-info

Usage

import { NetworkInfo } from "react-native-network-info";

// Get Local IP
NetworkInfo.getIPAddress().then(ipAddress => {
  console.log(ipAddress);
});

// Get IPv4 IP (priority: WiFi first, cellular second)
NetworkInfo.getIPV4Address().then(ipv4Address => {
  console.log(ipv4Address);
});

// Get Broadcast
NetworkInfo.getBroadcast().then(broadcast => {
  console.log(broadcast);
});

// Get SSID
NetworkInfo.getSSID().then(ssid => {
  console.log(ssid);
});

// Get BSSID
NetworkInfo.getBSSID().then(bssid => {
  console.log(bssid);
});

// Get Subnet
NetworkInfo.getSubnet().then(subnet => {
  console.log(subnet);
});

// Get Default Gateway IP
NetworkInfo.getGatewayIPAddress().then(defaultGateway => {
  console.log(defaultGateway);
});

// Get frequency (supported only for Android)
NetworkInfo.getFrequency().then(frequency => {
  console.log(frequency);
});

Manually Linking the Library

If react-native link fails and you are not using the auto linking provided in React Native >= 0.60

iOS

  1. In XCode, in the project navigator, right click Libraries ➜ Add Files to [your project's name]

  2. Go to node_modules ➜ react-native-network-info and add the .xcodeproj file

  3. Add RNNetworkInfo.a to Build Phases -> Link Binary With Libraries

Run your project (Cmd+R)

Android

  1. Add the following lines to android/settings.gradle:

    include ':react-native-network-info'
    project(':react-native-network-info').projectDir = new File(settingsDir, '../node_modules/react-native-network-info/android')
  2. Update the android build tools version to 2.2.+ in android/build.gradle:

    buildscript {
        ...
        dependencies {
            classpath 'com.android.tools.build:gradle:2.2.+' // <- USE 2.2.+ version
        }
        ...
    }
    ...
  3. Update the gradle version to 2.14.1 in android/gradle/wrapper/gradle-wrapper.properties:

    ...
    distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
  4. Add the compile line to the dependencies in android/app/build.gradle:

    dependencies {
        ...
        compile project(':react-native-network-info')
    }
  5. Add the import and link the package in MainApplication.java:

    import com.pusherman.networkinfo.RNNetworkInfoPackage; // <-- add this import
    
    public class MainApplication extends Application implements ReactApplication {
        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                new MainReactPackage(),
                new RNNetworkInfoPackage() // <-- add this line
            );
        }
    }

Dev Notes

Notes on how this package was made can be found here.

changelog

5.2.1 - 2019-09-23 (Yellow Check)

Bug Fix

  • (iOS) Fixed iOS compile error when auto linking (thanks @zusinShinpei)

5.2.0 - 2019-09-23 (Yellow Mark)

  • (iOS/Android) Added getGatewayIPAddress method (thanks @parrotmac)
  • (iOS/Android) Updated getIPV4Address to return WIFI IP if connected (thanks @Rapsssito)
  • (Android) Added getFrequency to get wifi frequency (thanks @antonkulyk)

5.1.0 - 2019-06-24 (The Upside Down)

  • (iOS/Android) Added getSubnet method (thanks @richardvclam)
  • (iOS) Added support for tvOS (thanks @richardvclam & @ChrisOdicho)

5.0.1 - 2019-05-25 (Teensy Weensy)

Bug Fix

  • (Android) Removed unused Android import (thanks @GuillermoRivera & @MaShizhen)

5.0.0 - 2019-05-25 (Itsy Bitsy)

Breaking Changes

  • (iOS/Android) Switched from callbacks to async methods (thanks @hosseinmd)
  • (Android) Migrated to AndroidX (thanks @rafcontreras)

Bug Fix

  • (Android) Updated dependency configurations for Android (thanks @robwalkerco)
  • (npm) Removed android/build from npm package to reduce size

4.0.1 - 2019-01-09 (Traveling On)

Bug Fix

  • (Android) Fix intellij-core (thanks @martinffx)
  • (Android) Ignore DS-LITE block of ip addresses (thanks @shashfrankenstien)
  • (iOS) Corrected import module (thanks @wuwen2333)

4.0.0 - 2018-09-04 (Walrus Tooth)

Breaking Changes

  • (Android) The string "error" will be returned if the library is unable to find the value requested to keep it consistent with the iOS library

Features

  • (Android) SupplicantState is now checked to fix a bug when wifi was turned off then back on (thanks @yosimasu)
  • Typescript declarations added (thanks @fitzpasd)

3.2.2 - 2018-04-06 (Desert Wine)

Bug Fix

  • (Android) Removed createJSModules to fix Android build

3.2.1 - 2018-04-04 (Refined Wine)

Bug Fix

  • (Android) Added google as a repo to build.gradle

3.2.0 - 2018-04-03 (Fine Wine)

Features

  • (iOS/Android) Added method for getting broadcast address (thanks @codlab)
  • (Android) Upgrade library dependencies (thanks @codlab)

3.1.0 - 2018-03-06 (Catnip)

Features

  • (iOS) Added support for getting IPv4 address (thanks @jgfidelis)

3.0.0 - 2017-08-07 (Flapjack)

Breaking Changes

  • Updated to support React Native 0.47

2.2.0 - 2017-05-23 (Juggernaut)

Features

  • (Android) Added method for getting IPv4 address (thanks @davidstoneham)

2.1.0 - 2017-04-28 (City Slicker)

Features

  • Added method for getting BSSID

2.0.0 - 2017-04-07 (Saki Bomb)

Breaking Changes

  • (JS) Switched to named exports

var NetworkInfo = require('react-native-network-info');

becomes

import { NetworkInfo } from 'react-native-network-info';;

Fixes

  • (Android) Switched to getApplicationContext() to prevent possible memory leaks
  • (Android) Updated getIPAddress so IP will be shown in the simulator

Features

  • Updated README to make the Installation instructions up to date

1.0.0 - 2017-03-17 (Ghostwriter)

Breaking Changes

  • Updated to support React Native 0.40.0

0.2.0 - 2015-11-21 (Pepperjack)

Breaking Changes

  • Updated to support React Native 0.14.2

Features

  • Added podspec support

0.1.1 - 2015-07-12 (Bees Knees)

Features

  • Updated README to include installation instructions

0.1.0 - 2015-07-12 (Lemurparty)

Features

  • Support for getting IP and SSID for Android and iOS